【问题标题】:Create a XmlDocument without attributes and first element创建一个没有属性和第一个元素的 XmlDocument
【发布时间】:2014-01-19 22:49:41
【问题描述】:

我想创建一个忽略第一行的 XmlDocument 对象,并删除所有其他元素的所有属性。 我该怎么做? xml 字符串和我正确的代码如下所示。

<?xml version="1.0" encoding="utf-8"?>
<boolean xmlns="http://tempuri.org/">true</boolean>

我使用的C#代码是:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);//xmlString is the value in snippet above this

【问题讨论】:

    标签: c# xml xmldocument


    【解决方案1】:

    我从this SO question 得出这个解决方案。这是一个完整的工作 MSTest 类。

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var xmlString = @"<?xml version=""1.0"" encoding=""utf-8""?>
                                <root>
                                    <boolean xmlns=""http://tempuri.org"" atr=""test"">true</boolean>
                                    <boolean xmlns=""http://tempuri.org"" atr=""test"">true</boolean>
                                    <boolean xmlns=""http://tempuri.org"" atr=""test"">true</boolean>
                                    <boolean xmlns=""http://tempuri.org"" atr=""test"">true</boolean>
                                </root>";
            var xElement = XElement.Parse(xmlString);
    
    
            var expectedXmlString = @"<root>
                                    <boolean>true</boolean>
                                    <boolean>true</boolean>
                                    <boolean>true</boolean>
                                    <boolean>true</boolean>
                                </root>";
            var expectedXElement = XElement.Parse(expectedXmlString);
    
            var actualXElement = stripAttributes(xElement);
    
            Assert.AreEqual(expectedXElement.ToString(), actualXElement.ToString());
        }
    
        static XElement stripAttributes(XElement root)
        {
            return new XElement(
                root.Name.LocalName,
                root.HasElements ?
                    root.Elements().Select(el => stripAttributes(el)) :
                    (object)root.Value
            );
        }
    }
    

    【讨论】:

      【解决方案2】:

      为了删除所有属性,只需在每个节点上调用.RemoveAllAttributes() 方法,但要小心:名称为xlmns 的属性不会被视为普通属性。这些是命名空间的一部分,您需要以不同的方式删除它们: Answer to: C# How to remove namespace information from XML elements

      string xmlPath = @"D:\test.xml";
      XDocument d = XDocument.Load(xmlPath);
      
      var allNodes = d.Descendants();
      
      foreach (var node in allNodes)
      {
          //Removes ALL attributes except namespace attributes like 'xmlns="..."'
          node.RemoveAttributes();
      }
      

      删除开头的声明:

      //Instead of using XDocument.Save() , use XmlWrite to Remove the declaration
      XmlWriterSettings xws = new XmlWriterSettings();
      xws.OmitXmlDeclaration = true;
      xws.Indent = true;
      using (var stream = File.Create(@"D:\testNew.xml"))
      using (XmlWriter xw = XmlWriter.Create(stream, xws))
      {
          d.Save(xw);
      }
      

      【讨论】:

      • 您可以在使用 XML 字符串而不是文件时发布代码吗?我无法使用 XML 字符串创建 XDocument。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2014-02-12
      • 2013-04-30
      • 2015-03-25
      • 2018-06-09
      • 2018-07-02
      相关资源
      最近更新 更多