简体   繁体   中英

Delete Attribute of Xml node c#

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>

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>

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

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

The reason you're getting an empty attribute - which xmlns="" refers to - is because your document's root node belongs to the namespace 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.

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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