How do I read this XML file?
-
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 -
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 -
They hide that information in the documention[^] Don't cross post.
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
-
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
-
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
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); -
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); -
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
-
Hopefully by know you have already tried that and discovered that you need to QName all the child nodes.
"ot:name/ot:firstname"