在C#中修改现有XML内容 [英] Modifying Existing XML Content in C#

查看:104
本文介绍了在C#中修改现有XML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了有关此主题的一些示例。一些示例提供了使用 SelectNodes() SelectSingleNode()修改属性的方法,而其他示例给出了该方法用 someElement.SetAttribute( attribute-name,新值)修改属性;

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");

但是我仍然很困惑,如果我只使用 XpathNodeItterator 怎么建立关系?

But I still confused that how to build the relation if I only used a XpathNodeItterator it?

假设我定义如下,

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)

推荐答案

来自 MSDN : XPathNavigator对象是通过实现IXPathNavigable接口,例如XPathDocument和XmlDocument类。由XPathDocument对象创建的 XPathNavigator对象是只读的,而由XmlDocument对象创建的XPathNavigator对象可以被编辑。XPathNavigator对象的只读或可编辑状态可以使用确定XPathNavigator类的CanEdit属性。

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."

因此,如果要设置属性,首先必须使用XmlDocument,而不是XPathDocument。

So, first of all you have to use XmlDocument, not XPathDocument, if you want to set an attribute.

显示如何使用XPathNavigator和XmlDocument的CreateNavigator方法修改XML数据的示例。 p://msdn.microsoft.com/zh-cn/library/zx28tfx1.aspx rel = nofollow noreferrer>此处。

An example of how to modify XML data using an XPathNavigator using the CreateNavigator method of an XmlDocument, is shown here.

当您从示例中可以看到,它的it.Current对象上有一个方法 SetValue

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:

        int vid = 2;
        var doc = new XmlDocument();
        doc.LoadXml("<Equipment><Items><SubItems  vid=\"1\" name=\"Foo\"/><SubItems vid=\"2\" name=\"Bar\"/></Items></Equipment>");
        var nav = doc.CreateNavigator();

        foreach (XPathNavigator it in nav.Select("/Equipment/Items/SubItems"))
        {
            if(it.MoveToAttribute("vid", it.NamespaceURI)) {
                int vidFromXML = int.Parse(it.Value);                    
                if (vidFromXML == vid)
                {
                    // if(it.MoveToNextAttribute() ... or be more explicit like the following:

                    if (it.MoveToParent() && it.MoveToAttribute("name", it.NamespaceURI))
                    {
                        it.SetValue("Two");
                    } else {
                        throw new XmlException("The name attribute was not found.");
                    }                
                }
            } else {
                    throw new XmlException("The vid attribute was not found.");
            }
        }

这篇关于在C#中修改现有XML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆