删除Xml节点C#的属性 [英] Delete Attribute of Xml node c#

查看:62
本文介绍了删除Xml节点C#的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的XML文件:

I have a XML file like this:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ua="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns="http://opcfoundation.org/UA/SDK/Configuration.xsd">
  <ServerConfiguration>
    <SecurityPolicies>
      <ServerSecurityPolicy>
        <SecurityMode>None_1</SecurityMode>
      </ServerSecurityPolicy>
    </SecurityPolicies>
  </ServerConfiguration>
</ApplicationConfiguration>

我想要通过代码添加更多名为ServerSecurityPolicy的节点.然后我使用以下代码:

What I want is to add more node named ServerSecurityPolicy by code. Then I use this code:

            string docaddress = "D:\\abc.xml";
            XDocument doc = XDocument.Load(docaddress);
            var root = doc.Root;
            var these = root.Descendants().Where(p => p.Name.LocalName == "SecurityPolicies");
            XElement addelement = new XElement("ServerSecurityPolicy");
            addelement.Add(new XElement("SecurityMode", "None_1"));           
            foreach (var elem in these)
            {
                elem.Add(addelement);
            }
            doc.Save(docaddress);

它实际上可以工作,但是新添加的节点是这样的:

It actually works, but the newly added node is something like this:

      <ServerSecurityPolicy xmlns="">
        <SecurityMode>None_1</SecurityMode>
      </ServerSecurityPolicy>

我不希望属性"xmlns",然后尝试通过以下方式将其删除:

I don't want the attribute "xmlns", then I try to delete it by something like this:

            var these2 = root.Descendants().Where(p => p.Name.LocalName == "ServerSecurityPolicy");
            foreach (var elem in these2)
            {
                    label2.Text += elem.Attribute("xmlns").Name.LocalName;
                    elem.Attribute("xmlns").Remove();
            }

label2.Text显示" xmlnsxmlnsxmlsn ..."因此我认为elem.Attribute("xmlns")指向我要删除的属性,但是Remove()无法正常工作.你能给我建议一些删除属性或添加没有属性的节点的方法吗?
谢谢

The label2.Text shows "xmlnsxmlnsxmlsn..." so that I think the elem.Attribute("xmlns") has pointed to the attributes I want to delete, but the Remove() not work. Can you suggest me some ways to delete the attribute or add node without attribute?
Thank you

推荐答案

问题是您要创建的元素实际上位于 http://opcfoundation.org/UA/SDK/Configuration.xsd 名称空间,由于根元素的这一部分,它是文档的默认名称:

The problem is that the element you want to create is actually in the http://opcfoundation.org/UA/SDK/Configuration.xsd namespace, which is the default for the document because of this part of the root element:

xmlns="http://opcfoundation.org/UA/SDK/Configuration.xsd"

您可以在该命名空间中轻松创建一个元素:

You can create an element in that namespace easily:

XNamespace configNs = "http://opcfoundation.org/UA/SDK/Configuration.xsd";
var these = root.Descendants(configNs + "ServerSecurityPolicy");

在文档中添加那个时,您会发现它不会添加 xmlns =" 部分,该部分之所以存在是因为您要添加没有命名空间的元素.

When you add that to your document, you'll find it won't add the xmlns="" part, which was present because you were adding an element without a namespace.

我还建议您也使用命名空间来查找元素:

I'd also suggest using the namespace to find elements too:

XNamespace configNs = "http://opcfoundation.org/UA/SDK/Configuration.xsd";
var these = root.Descendants(configNs + "SecurityPolicies");

在我看来,这比仅使用本地名称要干净得多.

That's much cleaner than just using the local name, in my view.

这篇关于删除Xml节点C#的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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