0

I'm using C# in reading an XML file and counting how many "elements" there are in an XML tag, like this for example...

<Languages>English, Deutsche, Francais</Languages>

there are 3 "elements" inside the Languages tag: English, Deutsche, and Francais . I need to know how to count them and return the value of how much elements there are. The contents of the tag have the possibility of changing over time, because the XML file has to expand/accommodate additional languages (whenever needed).

IF this is not possible, please do suggest workarounds for the problem. Thank you.

EDIT: I haven't come up with the code to read the XML file, but I'm also interested in learning how to.

EDIT 2: revisions made to question

Anthony
  • 437
  • 2
  • 8
  • 18

3 Answers3

5
string xml = @"<Languages>English, Deutsche, Francais</Languages>";
var doc = XDocument.Parse(xml);

string languages = doc.Elements("Languages").FirstOrDefault().Value;
int count = languages.Split(',').Count();

In response to your edits which indicate that you're not simply trying to pull out comma separated strings from an XML element, then your approach to storing the XML in the first place is incorrect. As another poster commented, it should be:

<Languages>
    <Language>English</Language>
    <Language>Deutsche</Language>
    <Language>Francais</Language>
</Languages>

Then, to get the count of languages:

string xml = @"<Languages>
                   <Language>English</Language>
                   <Language>Deutsche</Language> 
                   <Language>Francais</Language>
               </Languages>";

var doc = XDocument.Parse(xml);

int count = doc.Element("Languages").Elements().Count();
Scott
  • 13,735
  • 20
  • 94
  • 152
  • Sure, for your trivial example chaining `Value` and `FirstOrDefault` is OK, but wouldn't it better serve to show proper checking on the return of `FirstOrDefault`? – Kenneth K. Dec 18 '12 at 02:33
  • Sure, there are a lot of potential improvements, but as you said, this is a trivial example to illustrate the method in which the OP could go about getting the results he needs. – Scott Dec 18 '12 at 03:26
  • personally, I don't think this is the solution that I "need", because the contents of the `Languages` tag have the possibility of changing from time to time. But I'll check this out anyway. – Anthony Dec 18 '12 at 03:35
  • I answered the question based on the info you provided and you didn't mention that it could change. Edit the question with more detail and I'd be happy to revise my answer. – Scott Dec 18 '12 at 04:54
  • I've added more detail, hope that helps. – Anthony Dec 18 '12 at 05:38
  • hmmm, based on your new solution, it would appear that, If the languages in the `Languages` tag would increase in number, the `xml` string in the solution would also have to expand. Wouldn't that affect time complexity? – Anthony Dec 18 '12 at 23:03
3

First, an "ideal" solution: do not put more than one piece of information in a single tag. Rather, put each language in its own tag, like this:

<Languages>
    <Language>English</Language>
    <Language>Deutsche</Language>
    <Language>Francais</Language>
</Languages>

If this is not possible, retrieve the content of the tag with multiple languages, split using allLanguages.Split(',', ' '), and obtain the count by checking the length of the resultant array.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Ok, but just to be clear, an XML Element has a very specific meaning. In fact, the entire codeblock you have is an XML Element.

XElement xElm = new XElement("Languages", "English, Deutsche, Francais");
string[] elements = xElm.Value.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
rbtLong
  • 1,542
  • 3
  • 14
  • 31