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. Write an xml node in a specific position

Write an xml node in a specific position

Scheduled Pinned Locked Moved C#
csharpxml
7 Posts 2 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.
  • H Offline
    H Offline
    hellamasta
    wrote on last edited by
    #1

    Hi everybody, i'd like to know ('cause I'm on my first experience with c#) if it is possible with xmltextwriter, to write a new node in a specified position. Thank you

    H 1 Reply Last reply
    0
    • H hellamasta

      Hi everybody, i'd like to know ('cause I'm on my first experience with c#) if it is possible with xmltextwriter, to write a new node in a specified position. Thank you

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Not with an XmlTextWriter, no. That is used to merely write out XML forward-only, appending elements, fragments, etc. all moving forward. If you want to open an existing XML document and add an element in a certain position use the XmlDocument to load the XML and use one of the XPath methods like SelectSingleNode to find the node you want, then either prepend or append the node you want to add like so:

      class Program
      {
          static void Main(string\[\] args)
          {
              string xml = @"<?xml version=""1.0""?>
      

      <root>
      <child id=""one"">
      <grandchild id=""a""/>
      <grandchild id=""b""/>
      </child>
      <child id=""two"">
      <grandchild id=""c""/>
      <grandchild id=""d""/>
      </child>
      </root>";
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(xml);
       
      XmlNode node = doc.DocumentElement.SelectSingleNode(@"child[@id=""two""]");
      if (node != null)
      {
      XmlElement elem = doc.CreateElement("grandchild");
      elem.Attributes.Append(doc.CreateAttribute("id"));
      elem.Attributes[0].Value = "e";
      node.AppendChild(elem);
      }
       
      using (XmlTextWriter writer = new XmlTextWriter(Console.Out))
      {
      writer.Formatting = Formatting.Indented;
      doc.WriteTo(writer);
      }
      }
      }

      This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

      H 2 Replies Last reply
      0
      • H Heath Stewart

        Not with an XmlTextWriter, no. That is used to merely write out XML forward-only, appending elements, fragments, etc. all moving forward. If you want to open an existing XML document and add an element in a certain position use the XmlDocument to load the XML and use one of the XPath methods like SelectSingleNode to find the node you want, then either prepend or append the node you want to add like so:

        class Program
        {
            static void Main(string\[\] args)
            {
                string xml = @"<?xml version=""1.0""?>
        

        <root>
        <child id=""one"">
        <grandchild id=""a""/>
        <grandchild id=""b""/>
        </child>
        <child id=""two"">
        <grandchild id=""c""/>
        <grandchild id=""d""/>
        </child>
        </root>";
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
         
        XmlNode node = doc.DocumentElement.SelectSingleNode(@"child[@id=""two""]");
        if (node != null)
        {
        XmlElement elem = doc.CreateElement("grandchild");
        elem.Attributes.Append(doc.CreateAttribute("id"));
        elem.Attributes[0].Value = "e";
        node.AppendChild(elem);
        }
         
        using (XmlTextWriter writer = new XmlTextWriter(Console.Out))
        {
        writer.Formatting = Formatting.Indented;
        doc.WriteTo(writer);
        }
        }
        }

        This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

        H Offline
        H Offline
        hellamasta
        wrote on last edited by
        #3

        Thank you so much...in fact I was reading msdn documentation for xmlDataDocument...now I'll go trying...thanks agains

        H 1 Reply Last reply
        0
        • H Heath Stewart

          Not with an XmlTextWriter, no. That is used to merely write out XML forward-only, appending elements, fragments, etc. all moving forward. If you want to open an existing XML document and add an element in a certain position use the XmlDocument to load the XML and use one of the XPath methods like SelectSingleNode to find the node you want, then either prepend or append the node you want to add like so:

          class Program
          {
              static void Main(string\[\] args)
              {
                  string xml = @"<?xml version=""1.0""?>
          

          <root>
          <child id=""one"">
          <grandchild id=""a""/>
          <grandchild id=""b""/>
          </child>
          <child id=""two"">
          <grandchild id=""c""/>
          <grandchild id=""d""/>
          </child>
          </root>";
          XmlDocument doc = new XmlDocument();
          doc.LoadXml(xml);
           
          XmlNode node = doc.DocumentElement.SelectSingleNode(@"child[@id=""two""]");
          if (node != null)
          {
          XmlElement elem = doc.CreateElement("grandchild");
          elem.Attributes.Append(doc.CreateAttribute("id"));
          elem.Attributes[0].Value = "e";
          node.AppendChild(elem);
          }
           
          using (XmlTextWriter writer = new XmlTextWriter(Console.Out))
          {
          writer.Formatting = Formatting.Indented;
          doc.WriteTo(writer);
          }
          }
          }

          This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

          H Offline
          H Offline
          hellamasta
          wrote on last edited by
          #4

          well, now...i have the following structure of my xml file: - Now a I want to add dynamically new nodes under Variables, Parameters and BreakPoints, and for each subnode I have many properties to store, any suggestion?

          H 1 Reply Last reply
          0
          • H hellamasta

            well, now...i have the following structure of my xml file: - Now a I want to add dynamically new nodes under Variables, Parameters and BreakPoints, and for each subnode I have many properties to store, any suggestion?

            H Offline
            H Offline
            hellamasta
            wrote on last edited by
            #5
            H 1 Reply Last reply
            0
            • H hellamasta
              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              You need to change < to < and > to >, or check "Ignore HTML tags in this message" when posting SGML-derivative code like HTML and XML. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

              1 Reply Last reply
              0
              • H hellamasta

                Thank you so much...in fact I was reading msdn documentation for xmlDataDocument...now I'll go trying...thanks agains

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                Be aware that the XmlDataDocument is really designed for a DataSet or similar schema. If you're XML fragment is more complex you should stick with XmlDocument. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

                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