Results 1 to 2 of 2

Thread: how to read xmlnode value

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    757

    how to read xmlnode value

    Dear All,

    I need to read statuscode value of below XMLDoc.. how to do in C#?

    Code:
    <API version="1.0"><response>
    <operation name="ADD_REQUEST">
    <result>
    <statuscode>200</statuscode>
    <status>Success</status>
    <message>Request added successfully</message>
    </result>
    <Details>
    <workorderid>329</workorderid>
    </Details>
    <Details>
    <parameter>
    <name>workorderid</name>
    <value>329</value></parameter>
    </Details>
    </operation>
    </response>
    </API>

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,112

    Re: how to read xmlnode value

    I haven't done much C# or XML access, but have you tried a search. The first hit I got doing a search on: c# read xml
    was http://support.microsoft.com/kb/307548
    Modifying their example code to set a bool when reader.Name was equal to statuscode and print out the text on the next Text node showed the 200.
    Since I don't do XML, I don't know if this is the best example, but it worked.
    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                XmlTextReader reader = new XmlTextReader("C:\\c\\xmlTest.xml");
                bool readValue = false;
    
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element: // The node is an element.
                            readValue = (reader.Name == "statuscode");
                            break;
                        case XmlNodeType.Text: //Display the text in each element.
                            if (readValue)
                            Console.WriteLine(reader.Value);
                            break;
                        case XmlNodeType.EndElement: //Display the end of the element.
                            break;
                    }
                }
            }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width