Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore the White spaces and new lines in XML

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.

like image 672
pavan Avatar asked Feb 03 '26 01:02

pavan


1 Answers

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.

  1. First load the file
  2. Get all Catalog elements like you did
  3. Then select all the books of a catalog
  4. Get the books id & title
  5. Assign all the books from all the catalogs into one variable books as an array.

.

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();
like image 73
Chuck Savage Avatar answered Feb 04 '26 13:02

Chuck Savage