Edit XML document

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gator

    Edit XML document

    Hello,

    I am trying to edit a specific node in an XML file using C#. I have
    searched around for example code to do this. The only solutions I
    have come across either add a new node to the doc or have me re-write
    the entire doccument ... is there a way to open the doc, edit only a
    specific node and then save the changes?

    Here is an example of an XML document:

    <?xml version="1.0"?>
    <inventory>
    <book>
    <title>The Godfather</title>
    <author>Mario Puzo</author>
    <publisher>Sign et</publisher>
    <genre>Crime Drama</genre>
    <price>$29.95 </price>
    <stock>5</stock>
    </book>
    </inventory>

    How would I change only the "stock" node to "4" and save the file?

    Thanks for your help,
    Steve
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: Edit XML document

    gator wrote:
    I am trying to edit a specific node in an XML file using C#. I have
    searched around for example code to do this. The only solutions I
    have come across either add a new node to the doc or have me re-write
    the entire doccument ... is there a way to open the doc, edit only a
    specific node and then save the changes?
    >
    Here is an example of an XML document:
    >
    <?xml version="1.0"?>
    <inventory>
    <book>
    <title>The Godfather</title>
    <author>Mario Puzo</author>
    <publisher>Sign et</publisher>
    <genre>Crime Drama</genre>
    <price>$29.95 </price>
    <stock>5</stock>
    </book>
    </inventory>
    >
    How would I change only the "stock" node to "4" and save the file?
    Read the XML file into an XmlDocument, update the node and write it back
    to a file.

    Arne

    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: Edit XML document

      Arne Vajhøj wrote:
      gator wrote:
      >I am trying to edit a specific node in an XML file using C#. I have
      >searched around for example code to do this. The only solutions I
      >have come across either add a new node to the doc or have me re-write
      >the entire doccument ... is there a way to open the doc, edit only a
      >specific node and then save the changes?
      >>
      >Here is an example of an XML document:
      >>
      ><?xml version="1.0"?>
      ><inventory>
      > <book>
      > <title>The Godfather</title>
      > <author>Mario Puzo</author>
      > <publisher>Sign et</publisher>
      > <genre>Crime Drama</genre>
      > <price>$29.95 </price>
      > <stock>5</stock>
      > </book>
      ></inventory>
      >>
      >How would I change only the "stock" node to "4" and save the file?
      >
      Read the XML file into an XmlDocument, update the node and write it back
      to a file.
      Simple code:

      XmlDocument doc = new XmlDocument();
      doc.Load(@"C:\i .xml");
      doc.SelectSingl eNode("//inventory/book/stock/text()").Value
      = "4";
      doc.Save(@"C:\o .xml");

      Arne

      Comment

      • gator

        #4
        Re: Edit XML document

        On Jun 21, 10:46 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
        Arne Vajhøj wrote:
        gator wrote:
        I am trying to edit a specific node in an XML file using C#.  I have
        searched around for example code to do this.  The only solutions I
        have come across either add a new node to the doc or have me re-write
        the entire doccument ... is there a way to open the doc, edit only a
        specific node and then save the changes?
        >
        Here is an example of an XML document:
        >
        <?xml version="1.0"?>
        <inventory>
            <book>
                <title>The Godfather</title>
                <author>Mario Puzo</author>
                <publisher>Sign et</publisher>
                <genre>Crime Drama</genre>
                <price>$29.95 </price>
                <stock>5</stock>
            </book>
        </inventory>
        >
        How would I change only the "stock" node to "4" and save the file?
        >
        Read the XML file into an XmlDocument, update the node and write it back
        to a file.
        >
        Simple code:
        >
                     XmlDocument doc = new XmlDocument();
                     doc.Load(@"C:\i .xml");
                     doc.SelectSingl eNode("//inventory/book/stock/text()").Value
        = "4";
                     doc.Save(@"C:\o .xml");
        >
        Arne- Hide quoted text -
        >
        - Show quoted text -
        Arne,

        Yes! That's exactly what I needed. Thanks for the help AND the very
        quick response!!

        Steve

        Comment

        • Pavel Minaev

          #5
          Re: Edit XML document

          On Jun 22, 6:37 am, gator <glb...@gmail.c omwrote:
          Hello,
          >
          I am trying to edit a specific node in an XML file using C#.  I have
          searched around for example code to do this.  The only solutions I
          have come across either add a new node to the doc or have me re-write
          the entire doccument ... is there a way to open the doc, edit only a
          specific node and then save the changes?
          >
          Here is an example of an XML document:
          >
          <?xml version="1.0"?>
          <inventory>
              <book>
                  <title>The Godfather</title>
                  <author>Mario Puzo</author>
                  <publisher>Sign et</publisher>
                  <genre>Crime Drama</genre>
                  <price>$29.95 </price>
                  <stock>5</stock>
              </book>
          </inventory>
          >
          How would I change only the "stock" node to "4" and save the file?
          Note that the proposed solution still recreates the entire file when
          Save() is called - if you have originally mentioned it because of some
          performance requirements, then it might not be the one to seek.

          Also, it loads the entire XML file into memory. Again, it most likely
          doesn't matter, but for multimegabyte documents, using XmlReader/
          XmlWriter combo, passing through all elements unchanged, and only
          modifying the values you need, will be much more memory-efficient.

          Comment

          Working...