2

I want to create a XmlDocument object ignoring the first line and also remove all the attributes of all other elements. How would I do this? The xml string and the code I have right is as given below.

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

The code in C# I am using is:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);//xmlString is the value in snippet above this
Sunil
  • 20,653
  • 28
  • 112
  • 197

2 Answers2

1

in order to Remove all Attributes just call the .RemoveAllAttributes() method on every Node, but be careful: Attributes with the name xlmns are not treated like normal attributes. These are part of the namespace-stuff, and you need to remove them in a different way: 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();
}

To remove the declaration at the beginning:

//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);
}
Community
  • 1
  • 1
Philip W
  • 781
  • 3
  • 7
  • Can you post code when using an XML string and not a file? I cannot create XDocument with a XML string. – Sunil Jan 20 '14 at 01:13
  • Use [`XDocument.Parse`](http://msdn.microsoft.com/en-us/library/bb345532(v=vs.110).aspx) – KyleMit Jan 20 '14 at 01:38
1

I derived this solution from this SO question. Here's a full working MSTest class.

[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
        );
    }
}
Community
  • 1
  • 1
Aaron Palmer
  • 8,912
  • 9
  • 48
  • 77