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. Editing an XML node in C#

Editing an XML node in C#

Scheduled Pinned Locked Moved C#
csharpxmlc++databasecom
3 Posts 3 Posters 0 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.
  • A Offline
    A Offline
    AnneThorne
    wrote on last edited by
    #1

    Hello, We are building a windows desktop application using C# that reads in an xml file, to create a user interface. The user then enters values into the user interface. We want to be able to save an xml file with the same schema as the original xml file, but with the users entered values into it. Here is a simple example as part of input document: Needs Cleaning radio yes no We would want the output document to contain: Needs Cleaning radio yes yes no or perhaps Needs Cleaning text yes We aren't too adept at xmlTextReaders and xmlTextWriters yet, and have been doing some research in Google. There was an interesting article that seemed pertinent to this, but it dealt with C++ : http://www.codeproject.com/csharp/dcinsertxml.asp Any help would be appreciated. Thank you! Anne

    L C 2 Replies Last reply
    0
    • A AnneThorne

      Hello, We are building a windows desktop application using C# that reads in an xml file, to create a user interface. The user then enters values into the user interface. We want to be able to save an xml file with the same schema as the original xml file, but with the users entered values into it. Here is a simple example as part of input document: Needs Cleaning radio yes no We would want the output document to contain: Needs Cleaning radio yes yes no or perhaps Needs Cleaning text yes We aren't too adept at xmlTextReaders and xmlTextWriters yet, and have been doing some research in Google. There was an interesting article that seemed pertinent to this, but it dealt with C++ : http://www.codeproject.com/csharp/dcinsertxml.asp Any help would be appreciated. Thank you! Anne

      L Offline
      L Offline
      LongRange Shooter
      wrote on last edited by
      #2

      The use of XmlTextReader and XmlTextWriter is more for loading and persisting the data. You would load the data into an XmlDocument and then modify it there. You should also be aware of the fact that your Xml is not well-formed. You cannot have value containing two instances of choicelist and choicevalue. You need to do something like

      <ListValues>
      <Values>
      <choiceList />
      <choiceValue />
      </Values>
      <Values>
      <choiceList />
      <choiceValue />
      </Values>
      </ListValues>

      IMHO -- Navigating the DOM is a PITA. I prefer to avoid it whenever possible....especially if your application is creating/destroying xml objects quite alot. (You see the Xml engine in 2002, 2003 VS leaks memory) A really simple approach ( I like KIS ) is to create an object that represents your data and use XmlSerializer to populate/persist the data. Then your access of the data is normal object technology and you are saved from actual node/child navigation. The sample below is only from shear memory (my VS machine is dead) so the exactness is not guarenteed.

      [Serializable()]
      public class lable
      {
      public string name{get; set;}
      public string type{get; set;}
      public Value value{get; set;}
      }
      [Serializable()]
      public class ValueList:List
      {
      }
      [Serializable()]
      public class Values
      {
      public string choiceList{ get; set; }
      public string choiceValue{ get; set; }
      }

      public class XmlAccessor
      {
      public lable GetData(string path);
      {
      using (FileStream stream = new FileStream(path, FileMode.Open))
      {
      XmlSerializer ser = new XmlSerializer(typeof(lable));
      lable Lable = (lable)ser.Deserialize(stream);
      }
      return Lable;
      }
      public void PutData( string path, lable item )
      {
      using (FileStream stream = new FileStream(path, FileMode.Create))
      {
      XmlSerializer ser = new XmlSerializer(typeof(lable));
      ser.Serialize(stream, item);
      }
      }

      My only recommendation....if possible I'd change the casing on the lables so that they are Pacal cased.

      1 Reply Last reply
      0
      • A AnneThorne

        Hello, We are building a windows desktop application using C# that reads in an xml file, to create a user interface. The user then enters values into the user interface. We want to be able to save an xml file with the same schema as the original xml file, but with the users entered values into it. Here is a simple example as part of input document: Needs Cleaning radio yes no We would want the output document to contain: Needs Cleaning radio yes yes no or perhaps Needs Cleaning text yes We aren't too adept at xmlTextReaders and xmlTextWriters yet, and have been doing some research in Google. There was an interesting article that seemed pertinent to this, but it dealt with C++ : http://www.codeproject.com/csharp/dcinsertxml.asp Any help would be appreciated. Thank you! Anne

        C Offline
        C Offline
        conrado7
        wrote on last edited by
        #3

        Maybe these piece of code will help you: 1) if you want create xml file with C# you can use example: XmlDocument myXml = new XmlDocument(); string xml_text = ""+ ""+ ""+ ""+ ""+ ""+ ""; myXml.LoadXml(xml_text); 2) if you want open xml document use exmaple: XmlDocument myXml = new XmlDocument(); XmlTextReader reader = new XmlTextReader(file_name); myXml.Load(reader); reader.Close(); 3) if you want save you xml document you can use example: XmlTextWriter writer = new XmlTextWriter("output.xml",null); writer.Formatting = Formatting.Indented; dokumentXml.Save(writer); writer.Close(); Best way to learn this is MSDN like http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vborixmlschemadesignerwalkthroughs.asp

        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