1

I don't how to extract the values from XML document, and am looking for some help as I'm new to C#

I am using XmlDocument and then XmlNodeList for fetching the particular XML document

Here is my code

XmlNodeList XMLList = doc.SelectNodes("/response/result/doc");

And my XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<response>
<result>
  <doc>
    <long name="LoadID">494</long>
    <long name="EventID">5557</long>
    <str name="XMLData"><TransactionDate>2014-05-28T14:17:31.2186777-06:00</TransactionDate><SubType>tblQM2222Components</SubType><IntegerValue title="ComponentID">11111</IntegerValue></str></doc>
  <doc>
    <long name="LoadID">774</long>
    <long name="EventID">5558</long>
    <str name="XMLData"><TransactionDate>2014-05-28T14:17:31.2186777-06:00</TransactionDate><SubType>tblQM2222Components</SubType><IntegerValue title="ComponentID">11111</IntegerValue></str></doc>
</result>
</response>

In this i have to fetch every the XMLData data that is under every doc tag and i have to fetch last doc tag EventID.

3 Answers 3

1
var xml = XDocument.Parse(xmlString);

var docs = xml.Root.Elements("doc");

var lastDocEventID = docs.Last()
                         .Elements("long")
                         .First(l => (string)l.Attribute("name") == "EventID")
                         .Value;

Console.WriteLine ("Last doc EventId: " +lastDocEventID);

foreach (var doc in docs)
{
    Console.WriteLine (doc.Element("str").Element("TransactionDate").Value);
}

prints:

Last doc EventId: 5558
2014-05-28T14:17:31.2186777-06:00
2014-05-28T14:17:31.2186777-06:00
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,for your valuable suggestion,but how to get the XMLDATA of each Doc Tag.can you please tell me the syntax like which you already given above
1

You can use two XPath expressions to select the nodes you want. To answer each part of your question in turn:

To select all of the XMLData nodes:

XmlNodeList XMLList 
      = doc.SelectNodes("/response/result/doc/str[@name='XMLData']");

To select the last EventId:

XmlNode lastEventIdNode = 
   doc.SelectSingleNode("/response/result/doc[position() = 
                          last()]/long[@name='EventID']");

If not all doc nodes are guaranteed to have an event id child node, then you can simply:

XmlNodeList eventIdNodes = 
    doc.SelectNodes("/response/result/doc[long[@name='EventID']]");
XmlNode lastNode = eventIdNodes[eventIdNodes.Count - 1];

That should give you what you've asked for.

Update;

If you want the XML data inside each strXml element, you can use the InnerXml property:

XmlNodeList xmlList 
      = doc.SelectNodes("/response/result/doc/str[@name='XMLData']");
foreach(XmlNode xmlStrNode in xmlList)
{
    string xmlInner = xmlStrNode.InnerXml;
}

Comments

0

There's one result tag short in your xml.

Try using this. It's cleaner too imho

XmlNodeList docs = doc.SelectSingleNode("response").SelectSingleNode("result").SelectNodes("doc");

Then you can use a combination of SelectSingleNode, InnerText, Value to get the data from each XmlNode in your list.

For example if you want the EventID from the first doc tag:

int eventID = int.Parse(docs[0].SelectSingleNode("long[@name='EventID']").InnerText);

2 Comments

Thanks,for your valuable suggestion,but how to get the EventID of each Doc Tag.can you please tell me the syntax like which you already given above
I edited my post to show a way to get the EventID from the first doc tag for example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.