-2

I have xml which contains nodes. I would like to count the distinct values for that particular node and store into a variable in C#.

My XML is like this

<?xml version="1.0" encoding="utf-8"?>
<Emp>
      <A.EMPLID>1</A.EMPLID>
      <A.Phone>1234</A.Phone>
</Emp>
<Emp>
      <A.EMPLID>2</A.EMPLID>
      <A.Phone>1234</A.Phone>
</Emp>
<Emp>
      <A.EMPLID>1</A.EMPLID>
      <A.Phone>1234</A.Phone>
</Emp>
<Emp>
      <A.EMPLID>3</A.EMPLID>
</Emp>

I am trying this C# but throwing an error

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("SAMPLE.xml");
XmlNodeList count = xmlDoc.SelectNodes(@"//Emp");

Not sure how to Count distinct Emplid. In tis case It will be '3'

maccettura
  • 10,514
  • 3
  • 28
  • 35
user2897967
  • 337
  • 2
  • 8
  • 24

2 Answers2

0

As @Richardissimo noted, every XML must have one (and only one) root element.

string str = @"
<root>
    <Emp>
            <A.EMPLID>1</A.EMPLID>
            <A.Phone>1234</A.Phone>
    </Emp>
    <Emp>
            <A.EMPLID>2</A.EMPLID>
            <A.Phone>1234</A.Phone>
    </Emp>
    <Emp>
            <A.EMPLID>1</A.EMPLID>
            <A.Phone>1234</A.Phone>
    </Emp>
    <Emp>
            <A.EMPLID>3</A.EMPLID>
    </Emp>
</root>";

var xml = XElement.Parse(str);
int count = xml.Elements().GroupBy(x => x.Element("A.EMPLID").Value).Count();
// count = 3
JohnyL
  • 6,894
  • 3
  • 22
  • 41
  • Thank you for reply! My Xml could be very long. So how I can move xmlDoc to String. – user2897967 May 07 '18 at 20:24
  • What is the size of XML file? LINQ-to-XML works only with in-memory XML, that's why loading big size XML isn't a good idea. The string variable is just for illustration purposes. To load external file, use `XElement.Load(fileName)`. – JohnyL May 07 '18 at 20:27
  • I added root in to a file and use the above code. But got an error at count - System.NullRefernceException – user2897967 May 07 '18 at 20:36
  • If I may offer a *gentle* criticism: this is a bit like giving an answer in Belgian French to a question posed in Swiss French. The question uses `XmlDocument` and this answer uses `XDocument`, which are 2 completely different and incompatible syntaxes for processing XML files in dot net. – Richardissimo May 07 '18 at 21:03
  • @Richardissimo Then it's a _bad_ criticism :) – JohnyL May 08 '18 at 04:48
  • To clarify, I'm not saying the two French languages are completely different/incompatible, but they do have differences in some localised dialects, like C# (French in my analogy) when talking about XML files, which was what I was trying to convey :). I was trying and failing ;) to say that the OP asked a question involving loading XML from a file using XmlDocument, and this answer uses XDocument to load from a string. – Richardissimo May 08 '18 at 06:14
  • You might [use an `XQuery library` like this](https://www.codeproject.com/Articles/24766/Using-Saxon-XSL-2-0-and-XQuery-1-0-in-NET) and make use of existing XQuery-functions like `dìstinct-values` or `distinct-nodes` – Shnugo May 08 '18 at 07:14
0

You were nearly there with your comment. Let me assume you have added a node called root around your xml values.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("SAMPLE.xml");
XmlNodeList count = xmlDoc.SelectNodes(@"//root/Emp/A.EMPLID");
int i = count.Cast<XmlNode>().Select(a => a.InnerText).Distinct().Count();
Richardissimo
  • 5,596
  • 2
  • 18
  • 36
  • @user2897967 The "Cast" in your comment was incorrect as the contents of an `XmlNodeList` are `XmlNode`s, and there is nothing in your XML file representing an XmlAttribute. – Richardissimo May 07 '18 at 20:41