Saturday, 3 January 2015

C# Working With Specific XML structure



I am trying to get some data from an XML document. I have no control over the schema. If it were up to me I would have chosen another schema. I am using C#'s XPATH library to get the data.


XML DOC



<Journals>
<name>Title of Journal</name>
<totalvolume>2</totalvolume>
<JournalList>
<Volume no="1">
<Journal>
<issue>01</issue>
<Title>Title 1</Title>
<date>1997-03-10</date>
<link>http://ift.tt/1tAnRTn;
</Journal>
<Journal>
<issue>02</issue>
<Title>Title 3</Title>
<date>1997-03-17</date>
<link>http://ift.tt/1tAnRTn;
</Journal>
</Volume>
</Volume no="2">
<Journal>
<issue>01</issue>
<Title>Title 1</Title>
<date>1999-01-01</date>
<link>http://ift.tt/1tAnRTn;
</Journal>
<Journal>
<issue>01</issue>
<Title>Title 2</Title>
<date>1999-01-08</date>
<link>http://ift.tt/1tAnRTn;
</Journal>
</Volume>
</JournalList>
</Journal>


I am trying to get all the data in the Volume 2 node. Here is what I tried so far:


C# Code:


protected void loadXML(string url) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(url);



string strQuery = "Volume[@no='2']";

XmlElement nodeList = xmlDoc.DocumentElement;
XmlNodeList JournalList = nodeList.SelectNodes(strQuery);

foreach (XmlElement Journal in JournalList)
{
XmlElement temp = Journal;
}
}


It seems there are no nodes in JournalList. Anyone? Thanks in advance/


No comments:

Post a Comment