none
XMLNamespaceManager issue RRS feed

  • Question

  • I am having an issue accessing the following XML with the NameSpace information. When  I remove the xmlns info this code works fine. What am I doing wrong? </alert>

    Thanks in advance.

    <alert xmlns="urn:oasis:names:tc:emergency:cap:1.2"
    xmlns:ns3="http://gov.fema.ipaws.services/capresponse"
    xmlns:ns4="http://gov.fema.ipaws.services/IPAWS_CAPService/"
    xmlns:ns2="http://gov.fema.ipaws.services/caprequest">

        <identifier>NWS-IDP-PROD-2302168-2166866</identifier>
        <sender>w-nws.webmaster@noaa.gov</sender>
        <sent>2017-04-02T09:58:58-05:00</sent>
        <status>Actual</status>
        <msgType>Alert</msgType>
        <scope>Public</scope>
        <code>IPAWSv1.0</code>
        <info>
            <category>Met</category>
            <event>Severe Thunderstorm Warning</event>
            <responseType>Shelter</responseType>
            <urgency>Immediate</urgency>
            <severity>Severe</severity>
            <certainty>Observed</certainty>
            <eventCode>
                <valueName>SAME</valueName>
                <value>SVR</value>
            </eventCode>
            <effective>2017-04-02T09:58:58-05:00</effective>
            <onset>2017-04-02T09:58:00-05:00</onset>
            <expires>2017-04-02T11:00:00-05:00</expires>
            <senderName>NWS Fort Worth TX</senderName>
            <headline>Severe Thunderstorm Warning issued April 2 at 9:58AM CDT expiring April 2 at 11:00AM CDT by NWS Fort Worth TX</headline>

    my code.........

                XmlDocument doc = new XmlDocument();
                XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
                ns.AddNamespace("ns", "urn:oasis:names:tc:emergency:cap:1.2");
                //         ns.AddNamespace("ns", "http://gov.fema.ipaws.services/capresponse");
                //          ns.AddNamespace("ns", "http://gov.fema.ipaws.services/IPAWS_CAPService/");
                //          ns.AddNamespace("ns", "http://gov.fema.ipaws.services/caprequest");
                //   doc.Load("C:\\Users\\Dan\\OneDrive\\TornadoGPS-Tracker\\StormXMLTest.txt");
                doc.Load("C:\\Users\\Dan\\OneDrive\\TornadoGPS-Tracker\\StormXML-NSTest.txt");
                XmlNode idNode = doc.SelectSingleNode("/alert", ns);
                tBWeatherData.Text = idNode.SelectSingleNode("info").InnerText;

      

    • Edited by DDL19 Friday, April 7, 2017 1:23 PM
    Friday, April 7, 2017 1:23 PM

Answers

  • Please use the code block option in the toolbar to paste code into the forums. The code you posted is not valid so it is hard to see whether it was a bad paste or an issue with not using the code block. As it stands you didn't post valid XML so it is hard to figure out where your code is going wrong.

    The issue is that you didn't prefix the element in your select. The namespace manager is looking in the XML document for the element you specified. But since you didn't specify the namespace prefix it won't map to elements in namespaces. Since you called your namespace "ns" when you added it to the namespace manager that is the prefix you'd put on the element name. Now when it tries to find a match it'll be looking for the alert element in the correct namespace.

    var node = doc.SelectSingleNode("/ns:alert", ns);

    Michael Taylor
    http://www.michaeltaylorp3.net

    Friday, April 7, 2017 2:25 PM
    Moderator

All replies

  • You are missing the name of the namespace in your XPATH:

    XmlNode idNode = doc.SelectSingleNode("/ns:alert", ns);

    tBWeatherData.Text = idNode.SelectSingleNode("ns:info", ns).InnerText

    The "ns" in "ns:info" is the "ns" that you wrote in after "AddNamespace(...". the ns in "..., ns)" is the variable that you used for the XmlNamespaeManager. To avoid confusion, it may be better to use different names for both of them instead of naming both "ns".

    Friday, April 7, 2017 2:09 PM
    Moderator
  • Please use the code block option in the toolbar to paste code into the forums. The code you posted is not valid so it is hard to see whether it was a bad paste or an issue with not using the code block. As it stands you didn't post valid XML so it is hard to figure out where your code is going wrong.

    The issue is that you didn't prefix the element in your select. The namespace manager is looking in the XML document for the element you specified. But since you didn't specify the namespace prefix it won't map to elements in namespaces. Since you called your namespace "ns" when you added it to the namespace manager that is the prefix you'd put on the element name. Now when it tries to find a match it'll be looking for the alert element in the correct namespace.

    var node = doc.SelectSingleNode("/ns:alert", ns);

    Michael Taylor
    http://www.michaeltaylorp3.net

    Friday, April 7, 2017 2:25 PM
    Moderator