Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Reading into XML section and getting same value again

Reading into XML section and getting same value again

Scheduled Pinned Locked Moved XML / XSL
debuggingxmlhelpquestionannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gareth H
    wrote on last edited by
    #1

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

    Code = A123
    Quantity = 10

    using (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)

    S L 2 Replies Last reply
    0
    • G Gareth H

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

      Code = A123
      Quantity = 10

      using (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)

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • G Gareth H

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

        Code = A123
        Quantity = 10

        using (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)

        L Offline
        L Offline
        Lakhan Pal Garg
        wrote on last edited by
        #3

        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[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups