Last post Dec 15, 2010 05:03 PM by Goalie35
Dec 08, 2010 11:59 AM|Goalie35|LINK
I'm creating my first LINQ to XML file which I describe in this post here:
I was able to create the file successfully however now, the recipient of this file wants me to include the following:
1) A total ROW count: This is not simply a count of all records, but rather all ROWS. So for instance, if you view my post in the link I included above, you'll see the first tag is "Interactions". This tag counts as 1 row. So I need to find the total
count of all rows in the file. This count then gets placed into the xml filename when I save it.
2) Append a carriage return: At the end of the file, they want me to include a carriage return. I tried doing this by adding the following but it didn't work:
How can I go about creating these 2 features?
Dec 08, 2010 12:36 PM|Martin_Honnen|LINK
I am afraid "row" count does not relate to XML or LINQ to XML unless you use XML or LINQ to XML specific terms. If you have an XDocument in variable named "doc" then you can use
to count all element nodes in that XDocument. If you have an XElement in a variable named "el" instead and you want to count the element itself and all its descendant element nodes then use
1 + el.Descendants().Count()
As for adding white space at the end, I think instead of using XElement you would need to use XDocument, then it should be possible to do e.g.
XDocument doc = new XDocument(new XElement("Test"), "\n");
Dec 09, 2010 10:21 PM|Decker Dong - MSFT|LINK
To your first question and the second one with a "strange" but "clear" way, have a try:))
List<string> contents = new List<string>();
//You can get the whole rows number: content.Count
//You can override your whole xml file by using File.WriteAllLines("xxx.xml",contents.ToArray());
Dec 15, 2010 05:03 PM|Goalie35|LINK
Thanks to both of you. Both answers helped me reach my solution!