• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Dotnet Stuff

  • Home
  • Articles
  • Tutorials
  • Forums
  • Career Advice
  • Jobs
  • .NET FAQs
  • News
  • Privacy Policy

How to remove Header of the XML file in C#?

Home › Forums › C# › How to remove Header of the XML file in C#?

Tagged: Remove-xml-header, xml-declaration, xml-header, xml-version

  • This topic has 1 reply, 2 voices, and was last updated 8 years, 3 months ago by Rajeev.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • January 2, 2017 at 4:46 pm #437
    Rohit Singh
    Participant

    How Can I remove Header an XML file in C# using XMLDocument?
    I have code as below,

    XmlDocument xmlDoc= new XmlDocument();
    xmlDoc.Load("../test.xml");

    XML document loaded has First line <?xml version=”1.0″ encoding=”UTF-8″?>
    I don’t want this line in my file, How I can remove this xml version information line from the xml doc.

    • This topic was modified 2 years, 8 months ago by Rajeev.
    • This topic was modified 2 years, 8 months ago by Rajeev.
    January 17, 2017 at 3:53 pm #441
    Rajeev
    Keymaster

    There are various ways we can remove the version information of the xml file from C#.

    1) Remove the XML header from an XML File using the RemoveChild() method of XmlDocument using foreach XmlNode

     XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("../test.xml");
    
    foreach (XmlNode n in xmlDoc)
    {
      if (n.NodeType == XmlNodeType.XmlDeclaration)
      {
         xmlDoc.RemoveChild(n);
      }
    }

    2) Remove the XML version info from an XML File by Removing the FirstChild of XmlDocument using the RemoveChild().

    Above mentioned if condition can be changed as below to remove the xml header information from an xml file.
    if we blindly remove the first child from xml without an if check , it may remove xml root itself if xml version information not present in the xml.

    if (xmlDoc.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
    {
       //Remove the xml version,if it is the first child of the doc.
        xmlDoc.RemoveChild(xmlDoc.FirstChild);
    }/

    3) Remove the XML header using string.Replace statement.
    If xmldoc is made in string format,we can simply remove the xml version info from the xml file using
    the string.Replace statement.

    string xmlDoc = stringXmlDoc.Replace("<!--?xml version=\"1.0\" encoding=\"UTF-8\" ?-->", "");

    If we are writing an xml document and don’t want to write xml declaration in the xml doc then use the ConformanceLevel and OmitXmlDeclaration properties.

       XmlWriter writer;
       writer.Settings = new XmlWriterSettings();
       writer.Settings.ConformanceLevel = ConformanceLevel.Fragment;
       writer.Settings.OmitXmlDeclaration = true;
    
    • This reply was modified 2 years, 8 months ago by Rajeev.
    • This reply was modified 2 years, 8 months ago by Rajeev.
    • This reply was modified 2 years, 8 months ago by Rajeev.
    • This reply was modified 2 years, 8 months ago by Rajeev.
    • This reply was modified 2 years, 8 months ago by Rajeev.
    • This reply was modified 2 years, 8 months ago by Rajeev.
    • This reply was modified 2 years, 8 months ago by Rajeev.
  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

Related Posts:

  • OWIN Authentication in .NET Core
  • Serializing and Deserializing JSON using Jsonconvertor in C#
  • What is CAP Theorem? | What is Brewer’s Theorem?
  • SOLID -Basic Software Design Principles
  • What is Interface Segregation Principle (ISP) in SOLID Design Principles?

Primary Sidebar

Recent Posts

  • OWIN Authentication in .NET Core
  • Serializing and Deserializing JSON using Jsonconvertor in C#
  • What is CAP Theorem? | What is Brewer’s Theorem?
  • SOLID -Basic Software Design Principles
  • What is Interface Segregation Principle (ISP) in SOLID Design Principles?
  • What is Single Responsibility Principle (SRP) in SOLID Design Priciples?
  • What is the Liskov Substitution Principle(LSP) in SOLID Design Principles?
  • Dependency inversion principle(DIP) in SOLID Design
  • What is the Open Closed Principle in SOLID Design Principles?
  • What is the Difference Between Database Partitioning and Sharding?

Recent Forum Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

© Copywright 2017 Dotnetstuffs All Rights Reserved