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. add Xml in C#

add Xml in C#

Scheduled Pinned Locked Moved C#
questioncsharpxml
14 Posts 3 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.
  • B bertcox

    Thx a lot! But their is still a mistake i can't find. XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name 'Paris']"); An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in system.xml.dll Additional information: System error. Do you know what is wrong with the SelectSingleNode? Thx

    P Offline
    P Offline
    Philip Fitzsimons
    wrote on last edited by
    #5

    opps typo by me. mised the equals out! XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name = 'Paris']");


    "When the only tool you have is a hammer, a sore thumb you will have."

    B 1 Reply Last reply
    0
    • P Philip Fitzsimons

      opps typo by me. mised the equals out! XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name = 'Paris']");


      "When the only tool you have is a hammer, a sore thumb you will have."

      B Offline
      B Offline
      bertcox
      wrote on last edited by
      #6

      Sorry to disturb you again but there is another mistake :-) He puts this line in yellow : parisNode.AppendChild(newStreetNode); An unhandled exception of type 'System.NullReferenceException' occurred in CountryAddEdit.exe Additional information: Object reference not set to an instance of an object. And is it possible to put Paris in a string so if i want to change another city i just have to change the string? Thx!

      P 1 Reply Last reply
      0
      • B bertcox

        Sorry to disturb you again but there is another mistake :-) He puts this line in yellow : parisNode.AppendChild(newStreetNode); An unhandled exception of type 'System.NullReferenceException' occurred in CountryAddEdit.exe Additional information: Object reference not set to an instance of an object. And is it possible to put Paris in a string so if i want to change another city i just have to change the string? Thx!

        P Offline
        P Offline
        Philip Fitzsimons
        wrote on last edited by
        #7

        this is because the SelectSingleNode is failing. might be either your code or the xml file - can you post them. the SelectSingleNode use a plain string, so you can use what ever you want for it...


        "When the only tool you have is a hammer, a sore thumb you will have."

        B 1 Reply Last reply
        0
        • P Philip Fitzsimons

          this is because the SelectSingleNode is failing. might be either your code or the xml file - can you post them. the SelectSingleNode use a plain string, so you can use what ever you want for it...


          "When the only tool you have is a hammer, a sore thumb you will have."

          B Offline
          B Offline
          bertcox
          wrote on last edited by
          #8

          string filename = @"c:\country.xml"; XmlDocument countryDocument = new XmlDocument(); countryDocument.Load(filename); XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name = 'Paris']"); XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",null); XmlAttribute newStreetNameAttr = countryDocument.CreateAttribute("Name"); newStreetNameAttr.Value = "my new street"; newStreetNode.Attributes.Append(newStreetNameAttr); XmlNode newStreetNameNode = countryDocument.CreateNode(XmlNodeType.Element,"Name",null); newStreetNameNode.InnerText = " Test 4 "; newStreetNode.AppendChild(newStreetNameNode); XmlNode newStreetAgeNode = countryDocument.CreateNode(XmlNodeType.Element,"Age",null); newStreetAgeNode.InnerText = " 42 "; newStreetNode.AppendChild(newStreetAgeNode); parisNode.AppendChild(newStreetNode); countryDocument.Save(filename); countryDocument.CreateNode(XmlNodeType.Element,"Name",null); I found this in the MSDN because there was a mistake there to but did i fill it wrong?

          P 1 Reply Last reply
          0
          • B bertcox

            string filename = @"c:\country.xml"; XmlDocument countryDocument = new XmlDocument(); countryDocument.Load(filename); XmlNode parisNode = countryDocument.SelectSingleNode("/Country/City[@Name = 'Paris']"); XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",null); XmlAttribute newStreetNameAttr = countryDocument.CreateAttribute("Name"); newStreetNameAttr.Value = "my new street"; newStreetNode.Attributes.Append(newStreetNameAttr); XmlNode newStreetNameNode = countryDocument.CreateNode(XmlNodeType.Element,"Name",null); newStreetNameNode.InnerText = " Test 4 "; newStreetNode.AppendChild(newStreetNameNode); XmlNode newStreetAgeNode = countryDocument.CreateNode(XmlNodeType.Element,"Age",null); newStreetAgeNode.InnerText = " 42 "; newStreetNode.AppendChild(newStreetAgeNode); parisNode.AppendChild(newStreetNode); countryDocument.Save(filename); countryDocument.CreateNode(XmlNodeType.Element,"Name",null); I found this in the MSDN because there was a mistake there to but did i fill it wrong?

            P Offline
            P Offline
            Philip Fitzsimons
            wrote on last edited by
            #9

            I should have just used CreateElement :) however, you can use CreateNode, but you don't need the null as we don't need an xml namespace. I suspect the problem is your xml file, can you post that up.


            "When the only tool you have is a hammer, a sore thumb you will have."

            B 1 Reply Last reply
            0
            • P Philip Fitzsimons

              I should have just used CreateElement :) however, you can use CreateNode, but you don't need the null as we don't need an xml namespace. I suspect the problem is your xml file, can you post that up.


              "When the only tool you have is a hammer, a sore thumb you will have."

              B Offline
              B Offline
              bertcox
              wrote on last edited by
              #10

              This is the XML file (looks good in internet explorer) Test 24 Test2 19 Test3 33

              P 1 Reply Last reply
              0
              • B bertcox

                This is the XML file (looks good in internet explorer) Test 24 Test2 19 Test3 33

                P Offline
                P Offline
                Philip Fitzsimons
                wrote on last edited by
                #11

                yes, I thought so, you have changed the case of "city" in your xml... so either change the SelectNode to be SelectSingleNode("/Country/city[@Name = 'Paris']"); or fix you xml to be "City".. rememeber XML is case sensitive, so its best to pick on style and stick to it..


                "When the only tool you have is a hammer, a sore thumb you will have."

                B 1 Reply Last reply
                0
                • P Philip Fitzsimons

                  yes, I thought so, you have changed the case of "city" in your xml... so either change the SelectNode to be SelectSingleNode("/Country/city[@Name = 'Paris']"); or fix you xml to be "City".. rememeber XML is case sensitive, so its best to pick on style and stick to it..


                  "When the only tool you have is a hammer, a sore thumb you will have."

                  B Offline
                  B Offline
                  bertcox
                  wrote on last edited by
                  #12

                  well done :-) the other problem: XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",null); I changed it into XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",""); Now it works :-D Thx!

                  P 1 Reply Last reply
                  0
                  • B bertcox

                    well done :-) the other problem: XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",null); I changed it into XmlNode newStreetNode = countryDocument.CreateNode(XmlNodeType.Element,"Street",""); Now it works :-D Thx!

                    P Offline
                    P Offline
                    Philip Fitzsimons
                    wrote on last edited by
                    #13

                    or you can use the short version: XmlNode newStreetNode = countryDocument.CreateElement("Street");


                    "When the only tool you have is a hammer, a sore thumb you will have."

                    B 1 Reply Last reply
                    0
                    • P Philip Fitzsimons

                      or you can use the short version: XmlNode newStreetNode = countryDocument.CreateElement("Street");


                      "When the only tool you have is a hammer, a sore thumb you will have."

                      B Offline
                      B Offline
                      bertcox
                      wrote on last edited by
                      #14

                      off course :-D thx again

                      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