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. How do I read this XML file?

How do I read this XML file?

Scheduled Pinned Locked Moved XML / XSL
csharpquestionxmltutorial
8 Posts 2 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.
  • M Offline
    M Offline
    MaWeRic
    wrote on last edited by
    #1

    I have an XML file looking like this (just an example): <?xml version="1.0" encoding="UTF-8" ?> <family> <name xmlns="http://www.opentrans.org/XMLSchema/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" type="standard"> <firstname>Tom</firstname> <lastname>Smith</lastname> </name> <name xmlns="http://www.opentrans.org/XMLSchema/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" type="standard"> <firstname>Dale</firstname> <lastname>Smith</lastname> </name> </family> I can read up the document as a XMLDocument and navigate through SelctNodes if the Namespace attribute is not present. I have understood that I need to specify some kind of namespace using a namespace manager. This is my code so far : 'Create objcts Dim m_xmld As XmlDocument Dim m_nodelist As XmlNodeList Dim m_node As XmlNode 'Create the XML Document m_xmld = New XmlDocument() 'Load the Xml file m_xmld.Load("D:\family.xml") 'Get the list of name nodes m_nodelist = m_xmld.SelectNodes("/family/name") 'How many nodes msgbox(m_nodelist.count) Anyone can fill in the missing parts for me? C# or VB.NET .. doesn't matter :D Regards // M

    L 1 Reply Last reply
    0
    • M MaWeRic

      I have an XML file looking like this (just an example): <?xml version="1.0" encoding="UTF-8" ?> <family> <name xmlns="http://www.opentrans.org/XMLSchema/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" type="standard"> <firstname>Tom</firstname> <lastname>Smith</lastname> </name> <name xmlns="http://www.opentrans.org/XMLSchema/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" type="standard"> <firstname>Dale</firstname> <lastname>Smith</lastname> </name> </family> I can read up the document as a XMLDocument and navigate through SelctNodes if the Namespace attribute is not present. I have understood that I need to specify some kind of namespace using a namespace manager. This is my code so far : 'Create objcts Dim m_xmld As XmlDocument Dim m_nodelist As XmlNodeList Dim m_node As XmlNode 'Create the XML Document m_xmld = New XmlDocument() 'Load the Xml file m_xmld.Load("D:\family.xml") 'Get the list of name nodes m_nodelist = m_xmld.SelectNodes("/family/name") 'How many nodes msgbox(m_nodelist.count) Anyone can fill in the missing parts for me? C# or VB.NET .. doesn't matter :D Regards // M

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      They hide that information in the documention[^] Don't cross post.

      M 1 Reply Last reply
      0
      • L led mike

        They hide that information in the documention[^] Don't cross post.

        M Offline
        M Offline
        MaWeRic
        wrote on last edited by
        #3

        I used the link and could get all my orders suing namespace. Under each namespace there is a lot of elements like and others. I loop through my orders without problems. But how do I select a child to a node using namespace? releative XPath in other words. My code now look like this:

                'Objects()
                Dim XDoc As New XmlDocument
                Dim Orders As XmlNodeList
        
                'Try to read doc
                XDoc.Load(ImportFile)
        
                'Create an XmlNamespaceManager for resolving namespaces.
                Dim XN As XmlNamespaceManager = New XmlNamespaceManager(XDoc.NameTable)
                XN.AddNamespace("Order", "http://www.opentrans.org/XMLSchema/1.0")
        
                'Get Orders from XML
                Orders = XDoc.SelectNodes("//Order:ORDER", XN)
        
                For Each Order As XmlNode In Orders
                   dim MyOrderId as string = Orders.SelectSingleNode("ORDER\_ID",XN).innerText
                next
        

        That doesn't work if I want the OrderID. If I use

        Order.SelectSingleNode("/Order:ORDER_DATE", XN)

        I get the first OrderID each time. Ideas? //M

        L 2 Replies Last reply
        0
        • M MaWeRic

          I used the link and could get all my orders suing namespace. Under each namespace there is a lot of elements like and others. I loop through my orders without problems. But how do I select a child to a node using namespace? releative XPath in other words. My code now look like this:

                  'Objects()
                  Dim XDoc As New XmlDocument
                  Dim Orders As XmlNodeList
          
                  'Try to read doc
                  XDoc.Load(ImportFile)
          
                  'Create an XmlNamespaceManager for resolving namespaces.
                  Dim XN As XmlNamespaceManager = New XmlNamespaceManager(XDoc.NameTable)
                  XN.AddNamespace("Order", "http://www.opentrans.org/XMLSchema/1.0")
          
                  'Get Orders from XML
                  Orders = XDoc.SelectNodes("//Order:ORDER", XN)
          
                  For Each Order As XmlNode In Orders
                     dim MyOrderId as string = Orders.SelectSingleNode("ORDER\_ID",XN).innerText
                  next
          

          That doesn't work if I want the OrderID. If I use

          Order.SelectSingleNode("/Order:ORDER_DATE", XN)

          I get the first OrderID each time. Ideas? //M

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          The XML you are now working with (based on your XPath statements) does not seem to match what you posted.

          1 Reply Last reply
          0
          • M MaWeRic

            I used the link and could get all my orders suing namespace. Under each namespace there is a lot of elements like and others. I loop through my orders without problems. But how do I select a child to a node using namespace? releative XPath in other words. My code now look like this:

                    'Objects()
                    Dim XDoc As New XmlDocument
                    Dim Orders As XmlNodeList
            
                    'Try to read doc
                    XDoc.Load(ImportFile)
            
                    'Create an XmlNamespaceManager for resolving namespaces.
                    Dim XN As XmlNamespaceManager = New XmlNamespaceManager(XDoc.NameTable)
                    XN.AddNamespace("Order", "http://www.opentrans.org/XMLSchema/1.0")
            
                    'Get Orders from XML
                    Orders = XDoc.SelectNodes("//Order:ORDER", XN)
            
                    For Each Order As XmlNode In Orders
                       dim MyOrderId as string = Orders.SelectSingleNode("ORDER\_ID",XN).innerText
                    next
            

            That doesn't work if I want the OrderID. If I use

            Order.SelectSingleNode("/Order:ORDER_DATE", XN)

            I get the first OrderID each time. Ideas? //M

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            Using your original posted XML this code outputs TomSmith as expected

            XmlDocument doc = new XmlDocument();
            doc.Load("c:\\Research\\Xml\\Namespaces.xml");
            XmlNamespaceManager man = new XmlNamespaceManager( doc.NameTable);
            man.AddNamespace("ot", "http://www.opentrans.org/XMLSchema/1.0");
            XmlNode fam = doc.SelectSingleNode("//family");
            XmlNode n = fam.SelectSingleNode("ot:name", man);
            Console.WriteLine(n.InnerText);

            M 1 Reply Last reply
            0
            • L led mike

              Using your original posted XML this code outputs TomSmith as expected

              XmlDocument doc = new XmlDocument();
              doc.Load("c:\\Research\\Xml\\Namespaces.xml");
              XmlNamespaceManager man = new XmlNamespaceManager( doc.NameTable);
              man.AddNamespace("ot", "http://www.opentrans.org/XMLSchema/1.0");
              XmlNode fam = doc.SelectSingleNode("//family");
              XmlNode n = fam.SelectSingleNode("ot:name", man);
              Console.WriteLine(n.InnerText);

              M Offline
              M Offline
              MaWeRic
              wrote on last edited by
              #6

              Wonderful .. Now lets say I want to go 1 steå under name You select the Family but under name I have firstname and lastname ... Can I do a SingleNodeSelect 2 steps down in the structure like "ot:name/firstname"? //h

              L 1 Reply Last reply
              0
              • M MaWeRic

                Wonderful .. Now lets say I want to go 1 steå under name You select the Family but under name I have firstname and lastname ... Can I do a SingleNodeSelect 2 steps down in the structure like "ot:name/firstname"? //h

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                Hopefully by know you have already tried that and discovered that you need to QName all the child nodes. "ot:name/ot:firstname"

                M 1 Reply Last reply
                0
                • L led mike

                  Hopefully by know you have already tried that and discovered that you need to QName all the child nodes. "ot:name/ot:firstname"

                  M Offline
                  M Offline
                  MaWeRic
                  wrote on last edited by
                  #8

                  Yep I had ;D Crazy But it worked ... Thanks all ... /m

                  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