【问题标题】:Modify existing xml data using xml document in C#在 C# 中使用 xml 文档修改现有的 xml 数据
【发布时间】:2013-10-01 14:17:04
【问题描述】:

我想修改下面给出的 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");

请给我解决方案..

【问题讨论】:

  • 你试过调试吗?
  • 它不会修改文件,因为它是一个无限循环!你有while (oldelem.ChildNodes.Count != 0),但循环内没有任何东西修改oldelem。所以循环永远不会终止。

标签: c# asp.net xml xmldocument


【解决方案1】:

添加和删除 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>

【讨论】:

    【解决方案2】:

    这是一个小而简单的例子,我用来更改我的 [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;
                                }
                            }
                        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多