read xml
-
I have an xml file which I would like to read and pull out it's contents as follows: xml sample: <persons> <person></person> <person> <value>x</value> <value>y</value> <value>z</value> </person> <person> <value>w</value> <value>u</value> </person> ... ... ... </persons> So as I read through the above xml, I would like to go through them one record at a time so I would like to see: record No xml 1 <person></person> 2 <person> <value>x</value> <value>y</value> <value>z</value> </person> 3 <person> <value>w</value> <value>u</value> </person> How can I code this please? Thanks
-
I have an xml file which I would like to read and pull out it's contents as follows: xml sample: <persons> <person></person> <person> <value>x</value> <value>y</value> <value>z</value> </person> <person> <value>w</value> <value>u</value> </person> ... ... ... </persons> So as I read through the above xml, I would like to go through them one record at a time so I would like to see: record No xml 1 <person></person> 2 <person> <value>x</value> <value>y</value> <value>z</value> </person> 3 <person> <value>w</value> <value>u</value> </person> How can I code this please? Thanks
Giorgi Dalakishvili #region signature My Articles / My Latest Article[^] / My blog[^] #endregion
-
I have an xml file which I would like to read and pull out it's contents as follows: xml sample: <persons> <person></person> <person> <value>x</value> <value>y</value> <value>z</value> </person> <person> <value>w</value> <value>u</value> </person> ... ... ... </persons> So as I read through the above xml, I would like to go through them one record at a time so I would like to see: record No xml 1 <person></person> 2 <person> <value>x</value> <value>y</value> <value>z</value> </person> 3 <person> <value>w</value> <value>u</value> </person> How can I code this please? Thanks
Try the code below: string xmlFilePath = @"C:\Persons.xml"; System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(xmlFilePath); System.Xml.XmlNode pNode = doc.SelectSingleNode("persons"); for (int i = 0; i < pNode.ChildNodes.Count; i++) { System.Xml.XmlNode personNode = pNode.ChildNodes[i]; for (int x = 0; x < personNode.ChildNodes.Count; x++) { string value = personNode.ChildNodes[x].InnerText; } } Hope this helps.
-
Try the code below: string xmlFilePath = @"C:\Persons.xml"; System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(xmlFilePath); System.Xml.XmlNode pNode = doc.SelectSingleNode("persons"); for (int i = 0; i < pNode.ChildNodes.Count; i++) { System.Xml.XmlNode personNode = pNode.ChildNodes[i]; for (int x = 0; x < personNode.ChildNodes.Count; x++) { string value = personNode.ChildNodes[x].InnerText; } } Hope this helps.
-
Look at the member methods of XmlDocument, read the documentation.
-
I have an xml file which I would like to read and pull out it's contents as follows: xml sample: <persons> <person></person> <person> <value>x</value> <value>y</value> <value>z</value> </person> <person> <value>w</value> <value>u</value> </person> ... ... ... </persons> So as I read through the above xml, I would like to go through them one record at a time so I would like to see: record No xml 1 <person></person> 2 <person> <value>x</value> <value>y</value> <value>z</value> </person> 3 <person> <value>w</value> <value>u</value> </person> How can I code this please? Thanks
If you can use .Net Framework 3.5, I suggest using LINQ http://msdn.microsoft.com/en-us/library/bb387098.aspx[^]