XML element, node .. whaa!!
-
Dear all, Probably is the answer pretty obvious, but I have been struggling with this for the past 2 days already, and it's making me frustrated. I got following XML file
<?xml version="1.0" encoding="ISO-8859-1"?> <things> <people> <person id="1"> <name>Bob</name> <age>25</age> <projects> <project x="123" y="123">ABC</project> <project x="53" y="149">DEF</project> <project x="13" y="111">FOO</project> <project x="463" y="435">BAR</project> </projects> </person> <person id="2"> <name>Steve</name> <age>100</age> </person> <person id="3"> <name>Maria</name> <age>23</age> <projects> <project x="1" y="1">HOT</project> </projects> </person> </people> <file> <name>filename.ext</name> </file> </things>
How can I add in C# a new person, and acess the "people" node.. I even cut away so much code, that I ended up with following, just to print out what's in there, and it even fails:doc = new XmlDocument(); doc.Load(this.filename); XmlElement rootPeople = doc.GetElementById("people"); XmlNodeList node = rootPeople.GetElementsByTagName("person"); for (int i = 0; i < node.Count; i++) { Console.WriteLine(node[i].InnerXml); }
I have been messing with Xmlnodelists, xmlelements, xmlwhatevers, but even after reading the first 2 pages of google results, it still doesn't seem to work.. Can somebody point me in the good direct -
Dear all, Probably is the answer pretty obvious, but I have been struggling with this for the past 2 days already, and it's making me frustrated. I got following XML file
<?xml version="1.0" encoding="ISO-8859-1"?> <things> <people> <person id="1"> <name>Bob</name> <age>25</age> <projects> <project x="123" y="123">ABC</project> <project x="53" y="149">DEF</project> <project x="13" y="111">FOO</project> <project x="463" y="435">BAR</project> </projects> </person> <person id="2"> <name>Steve</name> <age>100</age> </person> <person id="3"> <name>Maria</name> <age>23</age> <projects> <project x="1" y="1">HOT</project> </projects> </person> </people> <file> <name>filename.ext</name> </file> </things>
How can I add in C# a new person, and acess the "people" node.. I even cut away so much code, that I ended up with following, just to print out what's in there, and it even fails:doc = new XmlDocument(); doc.Load(this.filename); XmlElement rootPeople = doc.GetElementById("people"); XmlNodeList node = rootPeople.GetElementsByTagName("person"); for (int i = 0; i < node.Count; i++) { Console.WriteLine(node[i].InnerXml); }
I have been messing with Xmlnodelists, xmlelements, xmlwhatevers, but even after reading the first 2 pages of google results, it still doesn't seem to work.. Can somebody point me in the good directFirst of all, I suggest using the
SelectNodes
andSelectSingleNode
methods instead ofGetElementsByTagName
since both use XPath expressions and therefore allow specifying/restricting the selection in more detail. You can for example select the (first) people node by writingXmlNode peopleNode = doc.SelectSingleNode("//things/people");
To create a new person or more general new nodes in a XML document take a look at this Create New Nodes in the DOM[^].
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
First of all, I suggest using the
SelectNodes
andSelectSingleNode
methods instead ofGetElementsByTagName
since both use XPath expressions and therefore allow specifying/restricting the selection in more detail. You can for example select the (first) people node by writingXmlNode peopleNode = doc.SelectSingleNode("//things/people");
To create a new person or more general new nodes in a XML document take a look at this Create New Nodes in the DOM[^].
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
Thank you Stefan, works like a charm!! Seems that, after reading your signature.. the universe is building improved versions of me :) Thanks again.