I have an XML file where i will be looping throught the nodes and will get the values of the Attributes.
But along with the attribute values i am also getting some symbols(rectangular box) in the output. If i try removing the space between tags then it was fine.
In the below XML i am getting the innertext of title.
<catalog>
<book id="bk101">
<details>
<title>XML Developer's Guide</title>
</details>
</book>
</catalog>
It was fine if my input is like this:
<catalog>
<book id="bk101">
<details><title>XML Developer's Guide</title></details>
</book>
</catalog>
I am using C#
foreach (XmlNode catalogid in Xmlcontent.GetElementsByTagName("catalog"))
{
foreach (XmlNode bookid in catalogid)
{
foreach (XmlNode titleid in bookid)
{
string booktitle = titleid.InnerText.ToString();
}
}
}
Please suggest how can i ignore the White spaces and new lines in my output.
Are you restricted to using .Net 2.0? If so ignore the following.
If you can use .Net 3.5 then the following Linq-To-Xml will work for you.
.
XElement root = XElement.Load(file);
var catalogs = root.Descendants("catalog");
var books = catalogs.SelectMany(c =>
c.Descendants("book").Select(book => new
{
Id = book.Attribute("id").Value,
Title = book.Descendants("title").First().Value
}))
//.OrderBy(b => b.Title) // sort by title or id if you'd like.
.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With