<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1991" ISBN="1-861003-11-0">
      <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
</bookstore>
xml文件格式如上,如何修改元素名称/如何修改属性名称/如何删除属性?,并保证xml文件的其它内容不变!例如:如何把元素名book改为mingcheng/如何把属性名genre改为chubanshe/如何把属性publicationdate="1991"直接去掉?
得到如下效果:<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <mingcheng chubanshe="autobiography" ISBN="1-861003-11-0">
      <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </mingcheng>
</bookstore>请大家帮帮忙,谢了!

解决方案 »

  1.   

    本人原创,刚刚写的。
    public void EditXmlFile()
    {
          XmlDocument xmlDoc = new XmlDocument();
          xmlDoc.Load("yourFile");
          XmlNode root = xmlDoc.DocumentElement;
          //if (root.HasChildNodes)
          //{
          //  return node.ChildNodes;
          //}
          /// 
          EditNode(root, "Book", "mingcheng", xmlDoc);
          xmlDoc.Save("yourFile");
    }private void EditNode(XmlNode node, string newName, string oldName, XmlDocument xmlDoc)
        {
          if(node.Name == oldName)
          {
            XmlNode newNode = xmlDoc.CreateNode(newName, node.LocalName, node.NamespaceURI);
            foreach(XmlAttribute a in node.Attributes)
            {
              if (a.Name == "genre")
              {
                XmlAttribute newAttr = xmlDoc.CreateAttribute("chuBanShe");
                newAttr.Value = a.Value;
                newNode.Attributes.Append(newAttr);
              }
              else if(a.Name != "publicationdate")
              {
                newNode.Attributes.Append(a);
              }
            }        foreach(XmlNode n in node.ChildNodes)
            {
              newNode.AppendChild(n);
            }
            XmlNode parent = node.ParentNode;
            parent.ReplaceChild(newNode, node);
          }
        }
      

  2.   

    我忘了些下面的Code了。
    重贴一下EditNote().private void EditNode(XmlNode node, string newName, string oldName, XmlDocument xmlDoc)
        {
          if(node.Name == oldName)
          {
            XmlNode newNode = xmlDoc.CreateNode(newName, node.LocalName, node.NamespaceURI);
            foreach(XmlAttribute a in node.Attributes)
            {
              if (a.Name == "genre")
              {
                XmlAttribute newAttr = xmlDoc.CreateAttribute("chuBanShe");
                newAttr.Value = a.Value;
                newNode.Attributes.Append(newAttr);
              }
              else if(a.Name != "publicationdate")
              {
                newNode.Attributes.Append(a);
              }
            }        foreach(XmlNode n in node.ChildNodes)
            {
              newNode.AppendChild(n);
            }
            XmlNode parent = node.ParentNode;
            parent.ReplaceChild(newNode, node);
          }
          else if(node.HasChildNodes)
          {
            foreach(XmlNode n in node.ChildNodes)
            {
              EditNode(n, newName, oldName, xmlDoc);
            }
          }
        }
      

  3.   

    如果你知道Book永远都是BookStore的ChildNode,而BookStore永远是RootNote,你就不需要用递归了。
      

  4.   

    建议你可以考虑使用XSL来进行转换。
    这样在修改时比写到代码里容易的多,只要重新定义XSL文件即可
      

  5.   

     public void modifyNodeAttributeName(XmlNode theXmlNode, string attributeOldName, string attributeNewName)//修改某元素节点属性名
            {
                XmlNode newNode = xml_doc.CreateNode(theXmlNode.NodeType, attributeNewName, theXmlNode.NamespaceURI);            foreach (XmlAttribute theattri in theXmlNode.Attributes)
                {
                    if (theattri.Name == attributeOldName)
                    {
                        XmlAttribute newAttr = xml_doc.CreateAttribute(attributeNewName);
                        newAttr.Value = theattri.Value;
                        newNode.Attributes.Append(newAttr);
                    }
                    else
                    {
                        newNode.Attributes.Append(theattri);
                    }
                }            foreach (XmlNode n in theXmlNode.ChildNodes)
                {
                    newNode.AppendChild(n);
                }
                XmlNode parent = theXmlNode.ParentNode;
                parent.ReplaceChild(newNode, theXmlNode);
              
            }代码如上:为什么执行到
    foreach (XmlAttribute theattri in theXmlNode.Attributes)
                {
                    if (theattri.Name == attributeOldName)
                    {
                        XmlAttribute newAttr = xml_doc.CreateAttribute(attributeNewName);
                        newAttr.Value = theattri.Value;
                        newNode.Attributes.Append(newAttr);
                    }
                    else
                    {
                        newNode.Attributes.Append(theattri);
                    }
                }
    时,在foreach (XmlAttribute theattri in theXmlNode.Attributes)提示"集合已修改;可能无法执行枚举操作。"呢?
      

  6.   

    回楼上。
    我现在没有机子试。
    你试着得到 XmlAttributeCollection attributes = xmlNode.Attributes;
          再用foreach(XmlAttribute attri in attributes)
             {         }
      

  7.   

    请大家帮帮忙,为什么在foreach   (XmlAttribute   theattri   in   theXmlNode.Attributes)提示"集合已修改;可能无法执行枚举操作。"呢?
      

  8.   

    调试成功。注意,当我们Append一个XMLNode时,总是取第一个Node。
      {
          if (node.Name == oldName)
          {
            XmlNode newNode = xmlDoc.CreateNode(XmlNodeType.Element, newName, node.NamespaceURI);
            XmlAttributeCollection collection = node.Attributes;
            foreach (XmlAttribute a in collection)
            {
              if (a.Name == "genre")
              {
                XmlAttribute newAttr = xmlDoc.CreateAttribute("chuBanShe");
                newAttr.Value = a.Value;
                newNode.Attributes.Append(newAttr);
              }
              else if (a.Name != "publicationdate")
              {
                XmlAttribute newAttr = xmlDoc.CreateAttribute(a.Name);
                newAttr.Value = a.Value;
                newNode.Attributes.Append(newAttr);
              }
            }        XmlNodeList nodes = node.ChildNodes;
            for(int i = 0; i < nodes.Count; i++)
            {
              newNode.AppendChild(nodes[0]);  //always append the fist node in the XMLNodeList.
            }
          
            XmlNode parent = node.ParentNode;
            parent.ReplaceChild(newNode, node);
          }
          else if (node.HasChildNodes)
          {
            foreach (XmlNode n in node.ChildNodes)
            {
              EditNode(n, newName, oldName, xmlDoc);
            }
          }
        }