在 C# 中修改现有 XML 内容
我找到了一些关于这个主题的例子。一些示例给出了使用 SelectNodes()
或 SelectSingleNode()
修改属性的方法,其他示例给出了使用 someElement.SetAttribute(" attribute-name", "new value");
但我仍然很困惑,如果我只使用 XpathNodeItterator
如何构建关系?
假设我定义如下,
System.Xml.XPath.XPathDocument doc = new XPathDocument(xmlFile);
System.Xml.XPath.XPathNavigator nav = doc.CreateNavigator();
System.Xml.XPath.XPathNodeIterator it;
it = nav.Select("/Equipment/Items/SubItmes");
while (it.MoveNext())
{
name = it.Current.GetAttribute("name ", it.Current.NamespaceURI);
int vidFromXML = int.Parse(it.Current.GetAttribute("vid", it.Current.NamespaceURI));
if (vidFromXML = vid)
{
// How can I find the relation between it and element and node? I want to modify name attribute value.
}
}
是否有像 it.setAttribute(name, "newValue")
这样的方法?
I found some examples about this subject. Some of the examples gived a method to modify attribute with SelectNodes()
or SelectSingleNode()
, and others gived the method to modify attribute with someElement.SetAttribute("attribute-name", "new value");
But I still confused that how to build the relation if I only used a XpathNodeItterator it
?
Assumed I defined as below,
System.Xml.XPath.XPathDocument doc = new XPathDocument(xmlFile);
System.Xml.XPath.XPathNavigator nav = doc.CreateNavigator();
System.Xml.XPath.XPathNodeIterator it;
it = nav.Select("/Equipment/Items/SubItmes");
while (it.MoveNext())
{
name = it.Current.GetAttribute("name ", it.Current.NamespaceURI);
int vidFromXML = int.Parse(it.Current.GetAttribute("vid", it.Current.NamespaceURI));
if (vidFromXML = vid)
{
// How can I find the relation between it and element and node? I want to modify name attribute value.
}
}
Is there a method like it.setAttribute(name, "newValue")
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 MSDN:“XPathNavigator 对象是从实现 IXPathNavigable 接口的类(例如 XPathDocument 和 XmlDocument 类)创建的 XPathDocument 对象创建的 XPathNavigator 对象是只读的,而 XmlDocument 对象创建的 XPathNavigator 对象可以编辑。使用 XPathNavigator 类的 CanEdit 属性确定仅状态或可编辑状态。”
因此,如果要设置属性,首先必须使用 XmlDocument,而不是 XPathDocument。
显示了如何使用 XmlDocument 的 CreateNavigator 方法使用 XPathNavigator 修改 XML 数据的示例 这里。
正如您将从示例中看到的,it.Current 对象上有一个 SetValue 方法。
以下是对代码进行一些细微修改的方法:
From MSDN: "An XPathNavigator object is created from a class that implements the IXPathNavigable interface such as the XPathDocument and XmlDocument classes. XPathNavigator objects created by XPathDocument objects are read-only while XPathNavigator objects created by XmlDocument objects can be edited. An XPathNavigator object's read-only or editable status is determined using the CanEdit property of the XPathNavigator class."
So, first of all you have to use XmlDocument, not XPathDocument, if you want to set an attribute.
An example of how to modify XML data using an XPathNavigator using the CreateNavigator method of an XmlDocument, is shown here.
As you'll see from the example, there is a method SetValue on your it.Current object.
Here's how you would do it for your code, with some slight modifications:
我编写了一个扩展方法,为任何
XPathNavigator
提供SetAttribute
方法:I wrote an extension method that provides a
SetAttribute
method for anyXPathNavigator
: