Search code examples
c#xmlxsltxmldocument

Trying to read data in xml element


I have this XML:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <env:Header xmlns:env="http://www.w3.org/2003/05/soap-envelope">
        <wsa:Action>http://cdr.ffiec.gov/public/services/RetrieveFacsimileResponse</wsa:Action>
        <wsa:MessageID>urn:uuid:f1c95072-c1ab-44f4-9c8f-59a26e28fce8</wsa:MessageID>
        <wsa:RelatesTo>urn:uuid:0f4a548e-39ad-4b5f-be02-ad9eaae75e32</wsa:RelatesTo>
        <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
        <wsse:Security>
            <wsu:Timestamp wsu:Id="Timestamp-ee96d970-c821-455d-9f92-75085fcad001">
            <wsu:Created>2024-06-10T20:27:43Z</wsu:Created>
            <wsu:Expires>2024-06-10T20:32:43Z</wsu:Expires>
            </wsu:Timestamp>
        </wsse:Security>
    </env:Header>
    <soap:Body>
        <RetrieveFacsimileResponse xmlns="http://cdr.ffiec.gov/public/services">
            <RetrieveFacsimileResult>TestData==</RetrieveFacsimileResult>
        </RetrieveFacsimileResponse>
    </soap:Body>
</soap:Envelope>

I am trying to read the data inside RetrieveFacsimileResult like this:

XmlDocument MyDoc = new XmlDocument(); 
MyDoc.LoadXml(response.Content);
XmlNode MyNode = MyDoc.SelectSingleNode("xml/soap:Envelope/soap:Body/RetrieveFacsimileResponse/RetrieveFacsimileResult");

I am getting an error

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function

How to read the data inside RetrieveFacsimileResult?


Solution

  • Please try the following solution that is using LINQ to XML API. It is available in the .Net Framework since 2007.

    No need to use a long XPath expression, we are going directly to the XML element of our interest.

    Obviously, a default namespace needs to be taken into account.

    c#

    void Main()
    {
        XDocument xdoc = XDocument.Parse(@"<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'
                       xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
                       xmlns:xsd='http://www.w3.org/2001/XMLSchema'
                       xmlns:wsa='http://schemas.xmlsoap.org/ws/2004/08/addressing'
                       xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'
                       xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>
            <env:Header xmlns:env='http://www.w3.org/2003/05/soap-envelope'>
                <wsa:Action>http://cdr.ffiec.gov/public/services/RetrieveFacsimileResponse</wsa:Action>
                <wsa:MessageID>urn:uuid:f1c95072-c1ab-44f4-9c8f-59a26e28fce8</wsa:MessageID>
                <wsa:RelatesTo>urn:uuid:0f4a548e-39ad-4b5f-be02-ad9eaae75e32</wsa:RelatesTo>
                <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
                <wsse:Security>
                    <wsu:Timestamp wsu:Id='Timestamp-ee96d970-c821-455d-9f92-75085fcad001'>
                        <wsu:Created>2024-06-10T20:27:43Z</wsu:Created>
                        <wsu:Expires>2024-06-10T20:32:43Z</wsu:Expires>
                    </wsu:Timestamp>
                </wsse:Security>
            </env:Header>
            <soap:Body>
                <RetrieveFacsimileResponse xmlns='http://cdr.ffiec.gov/public/services'>
                    <RetrieveFacsimileResult>TestData==</RetrieveFacsimileResult>
                </RetrieveFacsimileResponse>
            </soap:Body>
        </soap:Envelope>");
    
        XNamespace ns = "http://cdr.ffiec.gov/public/services";
    
        var RetrieveFacsimileResult = xdoc.Descendants(ns + "RetrieveFacsimileResult")?
            .SingleOrDefault()?.Value;
    }
    

    Output

    TestData==