1

I want to write string text in the beginning of Xml file before parent node. please tell me how to write string text. above Xml nodes.

{
    XmlTextWriter writer = new XmlTextWriter("product.xml", System.Text.Encoding.UTF8);
    //string x="<!-- sitemap-generator-url="http://www.auditmypc.com/free-sitemap-generator.asp --> ";
    string y= "<!-- This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp -->";
    string z= "<!-- Audit My PC also offers free security tools to help keep you safe during internet travels -->";

    writer.WriteStartDocument(true);
    writer.Formatting = Formatting.Indented;
    writer.Indentation = 2;
    writer.WriteStartElement("urlset");
    createNode("1", "url 1", writer);
    createNode("2", "url 2", writer);
    createNode("3", "url 3", writer);
    createNode("4", "url 4", writer);
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Close();
    MessageBox.Show("XML File created ! ");
}

private void createNode(string pID, string pName, XmlTextWriter writer)
{
    writer.WriteStartElement("url");
    writer.WriteStartElement("loc");
    writer.WriteString(pID);
    writer.WriteEndElement();

    writer.WriteEndElement();
}
Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • Why did you tag this asp.net? You're potentially going to steer people away from answering your question if they don't know anything about asp.net. This has nothing to do with that. – rory.ap Oct 09 '15 at 13:27
  • And for that matter...why not tag it with xml, which would drive people *to* your question who know something about working with xml. – rory.ap Oct 09 '15 at 13:27

3 Answers3

0

You can use XmlTextWriter.WriteComment method:

XmlTextWriter writer = new XmlTextWriter("product.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.WriteComment("This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp");
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("urlset");

output:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--This sitemap was created using the free tool found here: http://www.auditmypc.com/free-sitemap-generator.asp-->
<urlset />

<?xml ?> tag should be first so we inserted this comment in the topmost place.

Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
0

StackOverflow won't let me comment on the question yet so I'll post here.

If the XML file already exists one option to prepend a line to the top is to read the entire file into memory or a new temporary file and then re-write the file starting with the line you wish to add to the top. Here are some other posts on prepending a line to a file:

Add a single line to the top of text file

C#: Prepending to beginning of a file

Community
  • 1
  • 1
Tyler Jennings
  • 8,761
  • 2
  • 44
  • 39
0

In fact, you can do this without XML classes:

    string strContent = null, additionalContent = "<!--AdditionalContent-->";
    if (File.Exists(@"yourfile"))
    {
        strContent = File.ReadAllText(@"yourfile");
        File.WriteAllText(@"yourfile", additionalContent + Environment.NewLine + strContent);
    }

Note that this approach should be used for reasonably small files. Otherwise you need to read the file in smaller chunks to fit into your memory.

Alpay
  • 1,350
  • 2
  • 24
  • 56