繁体   English   中英

使用C#中的xml文档修改现有xml数据

[英]Modify existing xml data using xml document in C#

我想修改下面给出的XML数据:

<?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>

所有元素值都应在运行时按照设置进行修改。...

为此,我使用了以下代码,但未修改数据。.请帮助我获得解决方案。

 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");

请给我解决方法..

添加和删​​除XmlNode并不是最干净的方法,但是我只修复了代码,我想这就是您想要的

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);
}

而且您的xml不正确,至少是示例

<?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>

这是一个简单的示例,我用它来更改[web.config]中的connectionString值。 希望对您有所帮助。 很容易适应您的代码;-)

                    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;
                            }
                        }
                    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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