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 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 Regards // MIn your example XML is this something you have created or been provided? Normally you wouldn't be declaring the xmlns on every node in the document.This only needs to be done once at the head of the file
Tom
Smith
Dale
SmithNow what you are trying to achieve when reading this file in? Just a list of name nodes and what they contain or are you trying to create objects from each node ie of Type Person as an example and then set the objects firstname/lastname properties or set said properties to instances of an object? When reading the file it would best to use the XMLReader class.
modified on Monday, January 26, 2009 8:55 AM
-
In your example XML is this something you have created or been provided? Normally you wouldn't be declaring the xmlns on every node in the document.This only needs to be done once at the head of the file
Tom
Smith
Dale
SmithNow what you are trying to achieve when reading this file in? Just a list of name nodes and what they contain or are you trying to create objects from each node ie of Type Person as an example and then set the objects firstname/lastname properties or set said properties to instances of an object? When reading the file it would best to use the XMLReader class.
modified on Monday, January 26, 2009 8:55 AM