user1556433
user1556433

Reputation:

How to delete first line of XML file using XMLDocument in C#?

I am reading an XML file in C# using XMLDocument. My code goes like this:

XmlDocument doc = new XmlDocument();
doc.Load(xmlSourceFile);

First line of my XML document is

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

I have to delete this line. How should I?

Upvotes: 30

Views: 44934

Answers (6)

Vladimir  Zelenov
Vladimir Zelenov

Reputation: 1071

A very quick and easier solution is using the DocumentElement property of the XmlDocument class:

XmlDocument doc = new XmlDocument();
doc.Load(xmlSourceFile);
Console.Out.Write(doc.DocumentElement.OuterXml);

Upvotes: 4

Elazar Neeman
Elazar Neeman

Reputation: 141

I needed to have an XML serialized string without the declaration header so the following code worked for me.

StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings {
    Indent = true,
    OmitXmlDeclaration = true, // this makes the trick :)
    IndentChars = "  ",
    NewLineChars = "\n",
    NewLineHandling = NewLineHandling.Replace
};
using (XmlWriter writer = XmlWriter.Create(sb, settings)) {
    doc.Save(writer);
}
return sb.ToString();

Upvotes: 14

Engin Ardı&#231;
Engin Ardı&#231;

Reputation: 2469

Alternatively you can use this;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
if (xmlDoc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
    xmlDoc.RemoveChild(xmlDoc.FirstChild);

Upvotes: 8

Ben Holland
Ben Holland

Reputation: 341

I understand the need to eliminate the XML Declaration; I'm working on a script that alters the content of an application's preferences.xml, and the application doesn't read the file properly if the declaration is there (not sure why the developers decided to omit the XML declaration).

Instead of messing around with the XML any longer, I just created a removeXMLdeclaration() method that reads the XML file and removes the first line, then rewrites it back simply using streamreaders/writers. It's lightning fast, and works great! I just call the method after I've done all my XML alteration to clean the file up once and for all.

Here is the code:

public void removeXMLdeclaration()
    {
        try
        {
            //Grab file
            StreamReader sr = new StreamReader(xmlPath);

            //Read first line and do nothing (i.e. eliminate XML declaration)
            sr.ReadLine();
            string body = null;
            string line = sr.ReadLine();
            while(line != null) // read file into body string
            {
                body += line + "\n";
                line = sr.ReadLine();
            }
            sr.Close(); //close file

            //Write all of the "body" to the same text file
            System.IO.File.WriteAllText(xmlPath, body);
        }
        catch (Exception e3)
        {
            MessageBox.Show(e3.Message);
        }

    }

Upvotes: 1

Manjeet Singh
Manjeet Singh

Reputation: 11

There is another way to close this file use file stream.

public void xyz ()
{
       FileStream file = new FileStream(xmlfilepath, FileMode.Open, FileAccess.Read);
       XmlDocument doc = new XmlDocument();
       doc.load(xmlfilepath);

      // do whatever you want to do with xml file

      //then close it by 
      file.close();
      File.Delete(xmlfilepath);
}

Upvotes: 0

Matt
Matt

Reputation: 6943

I don't see why you would want to remove that. But if it is required, you could try this:

XmlDocument doc = new XmlDocument();
doc.Load("something");

foreach (XmlNode node in doc)
{
    if (node.NodeType == XmlNodeType.XmlDeclaration)
    {
        doc.RemoveChild(node);
    }
}

or with LINQ:

var declarations = doc.ChildNodes.OfType<XmlNode>()
    .Where(x => x.NodeType == XmlNodeType.XmlDeclaration)
    .ToList();

declarations.ForEach(x => doc.RemoveChild(x));

Upvotes: 36

Related Questions