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. C#
  4. XML element, node .. whaa!!

XML element, node .. whaa!!

Scheduled Pinned Locked Moved C#
questioncsharpxmlannouncement
3 Posts 2 Posters 1 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
    Michael Hendrickx
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • M Michael Hendrickx

      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

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      First of all, I suggest using the SelectNodes and SelectSingleNode methods instead of GetElementsByTagName 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 writing

      XmlNode 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

      www.troschuetz.de

      M 1 Reply Last reply
      0
      • S Stefan Troschuetz

        First of all, I suggest using the SelectNodes and SelectSingleNode methods instead of GetElementsByTagName 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 writing

        XmlNode 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

        www.troschuetz.de

        M Offline
        M Offline
        Michael Hendrickx
        wrote on last edited by
        #3

        Thank you Stefan, works like a charm!! Seems that, after reading your signature.. the universe is building improved versions of me :) Thanks again.

        www.code.ae

        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