none
OmitXmlDeclaration with xDocument RRS feed

  • Question

  • Hello!
    Is there no easier way to save an XML without the header?
    We already have the save method in the doc.
    <?xml version="1.0" encoding="utf-8"?>
    //My Code:
    
    XDeclaration dec = new XDeclaration(null, null, null);   
    XElement localRoot = new XElement("MY_ROOT");
    
    
    XDocument doc = new XDocument(dec, localRoot);
    
    
    localRoot.Add(new XElement("FIELD",
    				  new XElement("NAME", new XAttribute("value", "PanelCode")),
    				  new XElement("VALUE", new XAttribute("value", "--")),
    				  new XElement("GROUP", new XAttribute("value", "3"))));
    doc.Save(Path.Combine(dBPathAbsolute, $"Instances\\JOB_{taskname}.XML"));
    
    
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;
    settings.Indent = true;
    StringWriter sw = new StringWriter();
    using (XmlWriter xw = XmlWriter.Create(sw, settings))
    {
    	doc.Save(xw);
    }
    string xmlContent = sw.ToString();
    
    using (StreamWriter writetext = new StreamWriter(Path.Combine(dBPathAbsolute, $"Instances\\TASK_{taskname}.XML"), false))
    {
    	writetext.WriteLine(xmlContent);
    }
    
    With best regards Markus
    Tuesday, July 2, 2019 4:15 PM

Answers

  • Hi Markus Freitag,

    Thanks for your feedback.

    You can try the following code:

                string path = @"xmlPath";
                string str = null;
                StreamReader sr = new StreamReader(path);
                XDocument doc = XDocument.Load(path);
                str = sr.ReadToEnd().Replace(doc.Declaration.ToString(), "");
                sr.Close();
                doc.Root.RemoveAll();
                StreamWriter sw = new StreamWriter(path);
                sw.Write(str);
                sw.Close();

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 4, 2019 10:06 AM
    Moderator

  • Hope it can help you.


    Hi Xingyu Zhao,

    sure and thanks.

    I must create the XML by code.

    For me was this the solution

    Have a nice weekend.

    Best regards Markus

    Friday, July 5, 2019 4:11 PM

All replies

  • Hi Markus Freitag, 

    Thank you for posting here.

    According to your question, I make a test on my side and save an XML without the header successfully.

    You can refer the following code:

            static void Main(string[] args)
            {
                string path = @"xmlpath";
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
    
                foreach (XmlNode node in doc)
                {
                    if (node.NodeType == XmlNodeType.XmlDeclaration)
                    {
                        doc.RemoveChild(node);
                        doc.Save(path);
                    }
                }
                Console.WriteLine("success");
                Console.ReadKey();
            }

    XML:

    Result of the test:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 3, 2019 7:04 AM
    Moderator
  • Hello,

    Yes looks good. Is there a way with XDocument?

    I use XDocument.

    Regards Markus


    Wednesday, July 3, 2019 5:12 PM
  • Hi Markus Freitag,

    Thanks for your feedback.

    You can try the following code:

                string path = @"xmlPath";
                string str = null;
                StreamReader sr = new StreamReader(path);
                XDocument doc = XDocument.Load(path);
                str = sr.ReadToEnd().Replace(doc.Declaration.ToString(), "");
                sr.Close();
                doc.Root.RemoveAll();
                StreamWriter sw = new StreamWriter(path);
                sw.Write(str);
                sw.Close();

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 4, 2019 10:06 AM
    Moderator

  • Hope it can help you.


    Hi Xingyu Zhao,

    sure and thanks.

    I must create the XML by code.

    For me was this the solution

    Have a nice weekend.

    Best regards Markus

    Friday, July 5, 2019 4:11 PM