简体   繁体   中英

Modify existing xml data using xml document in C#

I want to modify XML data given below:

<?xml version="1.0" encoding="utf-8"?> 
<allitems>   
  <item ID="17997" quantity="three">     
    <available>Y</available>     
    <last_modified_price>300</last_modified_price>     
    <edition>2008<edition>     
  <item>
  <item ID="18039" quantity="two">     
    <available>Y</available>     
    <last_modified_price>250</last_modified_price>     
    <edition>2010<edition>     
  <item>
</allitems>

all elements value should be modified as per set in runtime....

For this I used following code but data is not modified..please help me in getting solution.

 XmlDocument modifydoc = new XmlDocument();
 modifydoc.Load(@"E:\XMLapp\XMLstorageWEB\patrick\XMLFile1.xml");
 var root = modifydoc.GetElementsByTagName("allitems")[0];
 var oldelem = root.SelectSingleNode("item[@ID =" + txt_id.Text + "]");
 var newelem = modifydoc.CreateElement("item");
 root.ReplaceChild(newelem, oldelem);
 while (oldelem.ChildNodes.Count != 0)
  {
      XmlElement available= modifydoc.CreateElement("available");
       available.InnerText = ddl_available.SelectedItem.Text;
      XmlElement last_modified_price= modifydoc.CreateElement("last_modified_price");
       last_modified_price.InnerText = ddl_last_modified_price.SelectedItem.Text;
      XmlElement edition= modifydoc.CreateElement("edition");
      edition.InnerText = ddl_edition.SelectedItem.Text;
      newelem.AppendChild(available);
      newelem.AppendChild(last_modified_price);
      newelem.AppendChild(edition);
      modifydoc.DocumentElement.AppendChild(newelem);
  }
   while (oldelem.Attributes.Count != 0)
  {
     newelem.Attributes.Append(oldelem.Attributes[0]);
  }
   modifydoc.Save(@"E:\XMLapp\XMLstorageWEB\patrick\XMLFile1.xml");

please give me solution..

Not the cleanest way, to add and remove a XmlNode, but just fixing your code I think this is what you want

var txt_id = "17997";
XmlDocument modifydoc = new XmlDocument();
modifydoc.Load(@"c:\temp\so\1.xml");
var root = modifydoc.GetElementsByTagName("allitems")[0];
var oldelem = root.SelectSingleNode("item[@ID =" + txt_id + "]");
var newelem = modifydoc.CreateElement("item");
root.ReplaceChild(newelem, oldelem);

XmlElement available= modifydoc.CreateElement("available");
available.InnerText = "CambiameInnerText";
XmlElement last_modified_price= modifydoc.CreateElement("last_modified_price");
last_modified_price.InnerText = "LastModifed";
XmlElement edition= modifydoc.CreateElement("edition");
edition.InnerText = "SelectedItem";
newelem.AppendChild(available);
newelem.AppendChild(last_modified_price);
newelem.AppendChild(edition);
modifydoc.DocumentElement.AppendChild(newelem);


foreach (XmlAttribute attribute in oldelem.Attributes)
{
newelem.SetAttribute(attribute.Name, attribute.Value);
}

and your xml is not correct, at least the example

<?xml version="1.0" encoding="utf-8"?> 
<allitems>   
  <item ID="17997" quantity="three">     
    <available>Y</available>     
    <last_modified_price>300</last_modified_price>     
    <edition>2008</edition>     
  </item>
  <item ID="18039" quantity="two">     
    <available>Y</available>     
    <last_modified_price>250</last_modified_price>     
    <edition>2010</edition>     
  </item>
</allitems>

Here is the small and easy example , i use for change the connectionString value in my [web.config]. I hope it's can help you. So easy to adapt for your code ;-)

                    System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
                    myXmlDocument.Load("myFullPathWebConfig.xml");
                    foreach (System.Xml.XmlNode node in myXmlDocument["configuration"]["connectionStrings"])
                    {
                        if (node.Name == "add")
                        {
                            if (node.Attributes.GetNamedItem("name").Value == "SCI2ConnectionString")
                            {
                                node.Attributes.GetNamedItem("connectionString").Value = connectionString;
                            }
                        }
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM