Reading into XML section and getting same value again
-
I am reading in some XML using the below code. The problem is when i attempt to get out the values for each Item, i get the same values again, ie: Debug prints out
Code = A123
Quantity = 10Code = A123
Quantity = 10using (TextReader textReader = new StringReader(xml))
using (XmlReader xmlReader = new XmlTextReader(textReader))
{
XPathDocument xPathDocument = new XPathDocument(xmlReader);//Create navigator to get nodes out XPathNavigator xPathNavigator = xPathDocument.CreateNavigator(); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xPathNavigator.NameTable); xmlNamespaceManager.AddNamespace("ns", "OrderSchema"); //Get the order items XPathExpression xPathExpressionItems = xPathNavigator.Compile("//ns:Order//ns:Items"); xPathExpressionItems.SetContext(xmlNamespaceManager); XPathNodeIterator xPathNodeIteratorItems = xPathNavigator.Select(xPathExpressionItems); do { string code = xPathNodeIteratorItems.Current.SelectSingleNode("//ns:Code", xmlNamespaceManager).ToString(); string quantity = xPathNodeIteratorItems.Current.SelectSingleNode("//ns:Quantity", xmlNamespaceManager).ToString(); System.Diagnostics.Debug.WriteLine("code == " + code); System.Diagnostics.Debug.WriteLine("quantity == " + quantity); System.Diagnostics.Debug.WriteLine("----"); } while (xPathNodeIteratorItems.MoveNext());
}
Heres the xml i am reading in:
<?xml version="1.0" encoding="UTF-8" ?>
<Order>
<Items>
<Item>
<Code>A123</Code>
<Quantity>10</Quantity>
</Item>
<Item>
<Code>A456</Code>
<Quantity>5</Quantity>
</Item>
</Items>
</Order>Any ideas what i am doing wrong?
Regards, Gareth. (FKA gareth111)
-
I am reading in some XML using the below code. The problem is when i attempt to get out the values for each Item, i get the same values again, ie: Debug prints out
Code = A123
Quantity = 10Code = A123
Quantity = 10using (TextReader textReader = new StringReader(xml))
using (XmlReader xmlReader = new XmlTextReader(textReader))
{
XPathDocument xPathDocument = new XPathDocument(xmlReader);//Create navigator to get nodes out XPathNavigator xPathNavigator = xPathDocument.CreateNavigator(); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xPathNavigator.NameTable); xmlNamespaceManager.AddNamespace("ns", "OrderSchema"); //Get the order items XPathExpression xPathExpressionItems = xPathNavigator.Compile("//ns:Order//ns:Items"); xPathExpressionItems.SetContext(xmlNamespaceManager); XPathNodeIterator xPathNodeIteratorItems = xPathNavigator.Select(xPathExpressionItems); do { string code = xPathNodeIteratorItems.Current.SelectSingleNode("//ns:Code", xmlNamespaceManager).ToString(); string quantity = xPathNodeIteratorItems.Current.SelectSingleNode("//ns:Quantity", xmlNamespaceManager).ToString(); System.Diagnostics.Debug.WriteLine("code == " + code); System.Diagnostics.Debug.WriteLine("quantity == " + quantity); System.Diagnostics.Debug.WriteLine("----"); } while (xPathNodeIteratorItems.MoveNext());
}
Heres the xml i am reading in:
<?xml version="1.0" encoding="UTF-8" ?>
<Order>
<Items>
<Item>
<Code>A123</Code>
<Quantity>10</Quantity>
</Item>
<Item>
<Code>A456</Code>
<Quantity>5</Quantity>
</Item>
</Items>
</Order>Any ideas what i am doing wrong?
Regards, Gareth. (FKA gareth111)
The only thing that jumps out is that you use Current before calling MoveNext the first time - the documentation[^] says you need to call MoveNext before Current.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I am reading in some XML using the below code. The problem is when i attempt to get out the values for each Item, i get the same values again, ie: Debug prints out
Code = A123
Quantity = 10Code = A123
Quantity = 10using (TextReader textReader = new StringReader(xml))
using (XmlReader xmlReader = new XmlTextReader(textReader))
{
XPathDocument xPathDocument = new XPathDocument(xmlReader);//Create navigator to get nodes out XPathNavigator xPathNavigator = xPathDocument.CreateNavigator(); XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xPathNavigator.NameTable); xmlNamespaceManager.AddNamespace("ns", "OrderSchema"); //Get the order items XPathExpression xPathExpressionItems = xPathNavigator.Compile("//ns:Order//ns:Items"); xPathExpressionItems.SetContext(xmlNamespaceManager); XPathNodeIterator xPathNodeIteratorItems = xPathNavigator.Select(xPathExpressionItems); do { string code = xPathNodeIteratorItems.Current.SelectSingleNode("//ns:Code", xmlNamespaceManager).ToString(); string quantity = xPathNodeIteratorItems.Current.SelectSingleNode("//ns:Quantity", xmlNamespaceManager).ToString(); System.Diagnostics.Debug.WriteLine("code == " + code); System.Diagnostics.Debug.WriteLine("quantity == " + quantity); System.Diagnostics.Debug.WriteLine("----"); } while (xPathNodeIteratorItems.MoveNext());
}
Heres the xml i am reading in:
<?xml version="1.0" encoding="UTF-8" ?>
<Order>
<Items>
<Item>
<Code>A123</Code>
<Quantity>10</Quantity>
</Item>
<Item>
<Code>A456</Code>
<Quantity>5</Quantity>
</Item>
</Items>
</Order>Any ideas what i am doing wrong?
Regards, Gareth. (FKA gareth111)
Hi Gareth- Please check this method:
private void ReadXMLData()
{
XmlDocument xDoc=new XmlDocument();
xDoc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Order><Items><Item><Code>A123</Code><Quantity>10</Quantity></Item><Item><Code>A456</Code><Quantity>5</Quantity></Item></Items></Order>");
XmlNodeList xNodeList = xDoc.GetElementsByTagName("Item");
for (Int16 iCount = 0; iCount < xNodeList.Count; iCount++)
{
Response.Write(xNodeList[iCount].ChildNodes.Item(0).InnerText+ "<br/>");
Response.Write(xNodeList[iCount].ChildNodes.Item(1).InnerText + "<br/>");
Response.Write("-----<br/>");
}
}Thanks & Regards Lakhan Pal Garg Free Code Snippets http://lakhangarg.blogspot.com[^]