0

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

请给我解决方案..

4

2 回答 2

0

添加和删​​除 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>
于 2013-10-01T15:25:35.790 回答
0

这是一个小而简单的示例,我用于更改 [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;
                            }
                        }
                    }
于 2013-10-02T09:58:38.247 回答