6

I have this XML file:

<div>
  <span id="1">
    <a class="time">
      25 April 2013 - 11:41PM #3
    </a>
    <p class="post">
      Blog Page Created
    </p>
    <br/>
  </span>
</div>

I know this is the exact format for HTML, but still, this is a XML file.

My problem is that I want to create another span tag.

This tag should be added above the tag that already exists.

I tried some code but its all just a mess and now I am even more confused to how to achieve this.

I am doing C# ASP.NET.

The end output should look like this:

<div>
  <span id="2">
    <a class="time">
      25 April 2013 - 3:00PM #3
    </a>
    <p class="post">
      Blog Page Created
    </p>
    <br/>
  </span>
  <span id="1">
    <a class="time">
      25 April 2013 - 3:00PM #3
    </a>
    <p class="post">
      Blog Page Created
    </p>
    <br/>
  </span>
</div>
Fred
  • 2,402
  • 4
  • 31
  • 58
  • 1
    Please post what you've tried. – Madbreaks Apr 25 '13 at 21:49
  • You may also find http://stackoverflow.com/questions/3004670/insert-xml-node-before-specific-node-using-c-sharp useful depending upon which Xml implementation you are using (e.g. XmlDocument, XDocument). – dash Apr 25 '13 at 21:51
  • if you are doing it in web project i would recommend using views. here is how to render view to string http://stackoverflow.com/questions/483091/render-a-view-as-a-string – maxlego Apr 25 '13 at 22:11

2 Answers2

6

Ok so this is the answer, for anyone who wants to see:

//This list gets populated somewhere else with all the existing id's...
List<int> allId = new List<int>();
doc = new XmlDocument();
doc.Load(Server.MapPath("../data/blog.xml"));
XmlNodeList list = doc.SelectNodes("div/span");
int newId = 1;
for (int i = 0; i < list.Count + 1; i++)
{
    if (allId.Contains(newId))
    {
        newId++;
    }
    else
    {

        string blogText = txtCreateBlog.Text;
        string blogDate = DateTime.Now.Day + " " + DateTime.Now.ToString("MMMM") + " " + DateTime.Now.Year + " - " + DateTime.Now.ToString("hh:mm tt") + " #" + newId.ToString();

        XmlNode newNode = doc.SelectSingleNode("div/span[@id=\"1\"]");
        XmlElement span = doc.CreateElement("span");
        span.SetAttribute("id", newId.ToString());
        doc.DocumentElement.PrependChild(span);
        span.SelectSingleNode("div/span[@id=\"" + newId.ToString() + "\"]");

        XmlNode newNode2 = doc.SelectSingleNode("div/span[@id=\"" + newId.ToString() + "\"]");
        XmlElement a = doc.CreateElement("a");
        a.SetAttribute("class", "time");
        a.InnerText = blogDate.ToString();
        span.AppendChild(a);

        XmlNode newNode3 = doc.SelectSingleNode("div/span[@id=\"" + newId.ToString() + "\"]");
        XmlElement p = doc.CreateElement("p");
        p.SetAttribute("class", "post");
        p.InnerText = blogText.ToString();
        span.AppendChild(p);

        XmlNode newNode4 = doc.SelectSingleNode("div/span[@id=\"" + newId.ToString() + "\"]");
        XmlElement br = doc.CreateElement("br");
        span.AppendChild(br);

        doc.Save(Server.MapPath("../data/blog.xml"));
}
Fred
  • 2,402
  • 4
  • 31
  • 58
1

here's something to get you started

XmlDocument xDoc = new XmlDocument();
xDoc.Load("XMLFile1.xml");

XmlElement span = xDoc.CreateElement("span");

xDoc.DocumentElement.AppendChild(span);

xDoc.Save("XMLFile1.xml");

you can look at the XmlDocument documentation for more functionality

the linq-to-sql XDocument, works similarly.