Click to See Complete Forum and Search --> : XmlTextReader and Writer


hkullana
August 26th, 2007, 02:49 PM
hi,

i have the code


XmlTextWriter tr = new XmlTextWriter("Items.xml",null);
tr.Formatting = Formatting.Indented;
tr.WriteStartDocument();

tr.WriteStartElement("Items");

tr.WriteStartElement("books");
tr.WriteElementString("book1","harry");
tr.WriteElementString("book2","lotr");
tr.WriteEndElement();
tr.Flush();
tr.Close();


so it generates the xml code below


<?xml version="1.0" ?>
- <Items>
- <books>
<book1>harry</book1>
<book2>lotr</book2>
</books>
</Items>


Now i want to delete the tag <book1> and its text harry from the file. How can i do it? (after the deletion the xml file will look like this: )


<?xml version="1.0" ?>
- <Items>
- <books>
<book2>lotr</book2>
</books>
</Items>

creatorul
August 26th, 2007, 03:22 PM
You can not do that (the deletion) with XmlTextWriter. Use the DOM approach for that:
http://www.devsource.com/article2/0,1895,1934067,00.asp

JonnyPoet
August 26th, 2007, 05:34 PM
Have you tried
XmlTextWriter tr = new XmlTextWriter("Items.xml",null);
tr.Formatting = Formatting.Indented;
tr.WriteStartDocument();

tr.WriteStartElement("Items");

tr.WriteStartElement("books");
tr.WriteElementString("book2","lotr");
tr.WriteEndElement();
tr.Flush();
tr.Close();

This should create exactly the code you wanted.

hkullana
August 27th, 2007, 12:20 PM
thanks for the website that is exactly what i want. I have one more question


XmlNodeList nodeList = doc.SelectNodes("//Authors");


in the above code what is the meaning of the slashes?

it is written that "Each slash represents a level in the element hierarchy." But i cannot understand what it means.

creatorul
August 28th, 2007, 08:10 AM
That is an XPath expression that selects all the Authors nodes in the document.
http://www.w3schools.com/xpath/xpath_syntax.asp

// Selects nodes in the document from the current node that match the selection no matter where they are