<?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>请大家帮帮忙,谢了!
<!-- 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>请大家帮帮忙,谢了!
解决方案 »
- 在线等。。DevExpress GridControl 默认展开第一行下的节点
- wordApp_DocumentBeforeClose事件的问题
- C# 动态实现二级域名(在线等)
- 好像是没有进行属性刷新,该如何操作呢?请高手指教
- 怎样在datagridview添加一有数据行
- c# windows form 调试捕获错误解决方案
- option选中设置
- 在循环中如何判断按钮是否按下
- TCPListener最多支持多少个长连接
- 谁知道哪里有全套的VS。NET下载,能下载能安装的![email protected] 分数好说!
- 如何在sqlserver 2005中导入图片信息,让它可以随其他信息一起显示出来?
- 跪求小游戏代码(滑铁卢战役)
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);
}
}
重贴一下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);
}
}
}
这样在修改时比写到代码里容易的多,只要重新定义XSL文件即可
{
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)提示"集合已修改;可能无法执行枚举操作。"呢?
我现在没有机子试。
你试着得到 XmlAttributeCollection attributes = xmlNode.Attributes;
再用foreach(XmlAttribute attri in attributes)
{ }
{
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);
}
}
}