none
XMLReader of sub nodes...

    Question

  • I am able to get at the attribute data with the following code:

                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "alert"))
                    {
                        if (reader.HasAttributes)
                       //     tBXMLMessage.Text = reader.GetAttribute(3);

    How do I get at the values of :

    <identifier>

    <sender>

    <sent>

    <status>

    etc.....

    Here is a portion of my XML steam.

    Thanks in advance - Dan

    <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>



    • Edited by DDL19 Sunday, April 2, 2017 9:46 PM
    Sunday, April 2, 2017 9:43 PM

Answers

  • How do I get at the values of :

    <identifier>

    <sender>

    <sent>

    <status>

    etc.....

    Here is a portion of my XML steam.

    <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>

    Various ways. For example:

    using System;
    using System.Xml;
    
    namespace CS_XML_Reader_of_subnodes
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("XMLFile1.xml");            
                var root = xmlDoc.DocumentElement;
                foreach (XmlNode node in root.ChildNodes)
                {
                    if(node.Name != "info")
                        Console.WriteLine(node.Name + ": " + node.InnerXml);
                }
            }
        }
    }
    

    Output:

    identifier: NWS-IDP-PROD-2302168-2166866
    sender: w-nws.webmaster@noaa.gov
    sent: 2017-04-02T09:58:58-05:00
    status: Actual
    msgType: Alert
    scope: Public
    code: IPAWSv1.0

    Error checking omitted for sake of brevity. Add to taste.

    - Wayne

    • Marked as answer by DDL19 Monday, April 3, 2017 11:27 AM
    Monday, April 3, 2017 1:38 AM
  • Hi DDL19,

    Thank you for posting here.

    For your question, the values you want to get are child nodes not attributes.

    Please try the following code.

    static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(@"C:\Users\v-wezan\Desktop\N1.xml");           
    
                XmlNode root = doc.FirstChild;
    
                //Display the contents of the child nodes.
                if (root.HasChildNodes)
                {
                    for (int i = 0; i < root.ChildNodes.Count; i++)
                    {
                        Console.WriteLine("{0}: {1}", root.ChildNodes[i].Name, root.ChildNodes[i].InnerText);
                    }
                }
                Console.ReadKey();
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 3, 2017 6:15 AM
    Moderator

All replies

  • How do I get at the values of :

    <identifier>

    <sender>

    <sent>

    <status>

    etc.....

    Here is a portion of my XML steam.


    Does the information at this link help?

    XPath Examples
    https://msdn.microsoft.com/en-us/library/ms256086%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    - Wayne

    Sunday, April 2, 2017 10:09 PM
  • Thanks for the response.

    How would this be incorporated into the syntax I have above?

    Sunday, April 2, 2017 10:24 PM
  • How do I get at the values of :

    <identifier>

    <sender>

    <sent>

    <status>

    etc.....

    Here is a portion of my XML steam.

    <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>

    Various ways. For example:

    using System;
    using System.Xml;
    
    namespace CS_XML_Reader_of_subnodes
    {
        class Program
        {
            static void Main(string[] args)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("XMLFile1.xml");            
                var root = xmlDoc.DocumentElement;
                foreach (XmlNode node in root.ChildNodes)
                {
                    if(node.Name != "info")
                        Console.WriteLine(node.Name + ": " + node.InnerXml);
                }
            }
        }
    }
    

    Output:

    identifier: NWS-IDP-PROD-2302168-2166866
    sender: w-nws.webmaster@noaa.gov
    sent: 2017-04-02T09:58:58-05:00
    status: Actual
    msgType: Alert
    scope: Public
    code: IPAWSv1.0

    Error checking omitted for sake of brevity. Add to taste.

    - Wayne

    • Marked as answer by DDL19 Monday, April 3, 2017 11:27 AM
    Monday, April 3, 2017 1:38 AM
  • Hi DDL19,

    Thank you for posting here.

    For your question, the values you want to get are child nodes not attributes.

    Please try the following code.

    static void Main(string[] args)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(@"C:\Users\v-wezan\Desktop\N1.xml");           
    
                XmlNode root = doc.FirstChild;
    
                //Display the contents of the child nodes.
                if (root.HasChildNodes)
                {
                    for (int i = 0; i < root.ChildNodes.Count; i++)
                    {
                        Console.WriteLine("{0}: {1}", root.ChildNodes[i].Name, root.ChildNodes[i].InnerText);
                    }
                }
                Console.ReadKey();
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 3, 2017 6:15 AM
    Moderator
  • Thank you ! That worked great!
    Monday, April 3, 2017 11:25 AM