简体   繁体   English

删除 Xml 节点 c# 的属性

[英]Delete Attribute of Xml node c#

I have a XML file like this:我有一个像这样的 XML 文件:

<?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>

What I want is to add more node named ServerSecurityPolicy by code.我想要的是通过代码添加更多名为 ServerSecurityPolicy 的节点。 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>

I don't want the attribute "xmlns", then I try to delete it by something like this:我不想要属性“xmlns”,然后我尝试通过以下方式删除它:

            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();
            }

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. label2.Text 显示“xmlnsxmlnsxmlsn ...”,因此我认为 elem.Attribute("xmlns") 已指向我要删除的属性,但 Remove() 不起作用。 Can you suggest me some ways to delete the attribute or add node without attribute?你能建议我一些方法来删除属性或添加没有属性的节点吗?
Thank you谢谢

The reason you're getting an empty attribute - which xmlns="" refers to - is because your document's root node belongs to the namespace xsi .您获得空属性( xmlns=""所指的属性)的原因是因为您的文档的根节点属于命名空间xsi When you're adding a node without a namespace - as you're doing in your example - you're not actually binding it to the xsi namespace.当你添加一个没有命名空间的节点时——就像你在你的例子中所做的那样——你实际上并没有将它绑定到xsi命名空间。

To make your node part of the root namespace, replace要使您的节点成为根命名空间的一部分,请替换

new XElement("ServerSecurityPolicy")

with

new XElement("ServerSecurityPolicy",
              new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"))

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:问题是您要创建的元素实际上在http://opcfoundation.org/UA/SDK/Configuration.xsd命名空间中,这是文档的默认名称,因为这部分根元素:

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

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.当您将添加到您的文档中时,您会发现它不会添加xmlns=""部分,这是因为您添加了一个没有命名空间的元素。

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.在我看来,这比仅使用本地名称要干净得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM