猿问

C# 删除属性 XML

我试图从下面的 XML 文件示例代码中删除一些指定的属性。string[] szNodeList 是数组列表,因此节点包含字符串数组中的名称将被删除并再次保存


任何帮助将不胜感激。


        var doc = new System.Xml.XmlDocument();

        doc.Load("attrs.xml");

        var root = doc.DocumentElement;

        string[] szNodeList = new string[]  { "titleTextColor"

        ,"isLightTheme"

        ,"showText"

                    };

        foreach (System.Xml.XmlElement  child in root )

        {

            foreach (string sz in szNodeList)

            {

                root.RemoveAttribute(sz);

                //if (child.Attributes[sz] != null)

                //{

                //    child.Attributes.Remove(child.Attributes[sz]);

                //}

            }

        }


        doc.Save("build.xml");


    XML CODE


  <?xml version="1.0" encoding="utf-8"?>

  <resources>

    <attr name="cropImageStyle" format="reference" />

    <attr name="drawerArrowStyle" format="reference" />

    <attr name="height" format="dimension" />

    <attr name="isLightTheme" format="boolean" />

    <attr name="title" format="string" />

    <attr name="navigationMode">

      <enum name="listMode" value="1" />

      <enum name="normal" value="0" />

      <enum name="tabMode" value="2" />

    </attr>


  </resources>

但是保存为原始文件而不更改我删除的东西不起作用。


慕运维8079593
浏览 279回答 2
2回答

天涯尽头无女友

试试这个:doc&nbsp; &nbsp; // select all `resources/attr` node&nbsp; &nbsp; .SelectNodes("resources/attr")&nbsp; &nbsp; .Cast<XmlNode>()&nbsp; &nbsp; // that contains the `name` attribute whose value is in `szNodeList`&nbsp; &nbsp; .Where(x => !string.IsNullOrEmpty(x.Attributes["name"]?.Value) && szNodeList.Contains(x.Attributes["name"].Value))&nbsp; &nbsp; .ToList()&nbsp; &nbsp; // and, remove them from their parent&nbsp; &nbsp; .ForEach(x => x.ParentNode.RemoveChild(x));

大话西游666

这里的问题之一是术语。正如我所理解的,您不是在尝试删除属性- 您是在尝试根据属性的值删除整个元素name。如果您可以为此使用 LINQ to XML,我会这样做。它通常使使用 XML 变得更加容易。这是一个完整的程序来做你想做的事:using System;using System.Linq;using System.Xml.Linq;class Test{&nbsp; &nbsp; static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var namesToRemove = new[]&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "titleTextColor",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "isLightTheme",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "showText"&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Load("test.xml");&nbsp; &nbsp; &nbsp; &nbsp; // For all the elements directly under the document root...&nbsp; &nbsp; &nbsp; &nbsp; doc.Root.Elements()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Where the array contains the value of the "name" attribute...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(x => namesToRemove.Contains((string) x.Attribute("name")))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove them from the document&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Remove();&nbsp; &nbsp; &nbsp; &nbsp; doc.Save("output.xml");&nbsp; &nbsp; }}输出:<?xml version="1.0" encoding="utf-8"?><resources>&nbsp; <attr name="cropImageStyle" format="reference" />&nbsp; <attr name="drawerArrowStyle" format="reference" />&nbsp; <attr name="height" format="dimension" />&nbsp; <attr name="title" format="string" />&nbsp; <attr name="navigationMode">&nbsp; &nbsp; <enum name="listMode" value="1" />&nbsp; &nbsp; <enum name="normal" value="0" />&nbsp; &nbsp; <enum name="tabMode" value="2" />&nbsp; </attr></resources>
随时随地看视频慕课网APP
我要回答