5

I have following XML file, i want to know best way to read this XML file

<MyFile> 
  <Companies> 
    <Company>123</Company> 
    <Company>456</Company>
    <Company>789</Company> 
  </Companies> 
</MyFile>

As an output i need collection of values like "123,456,789" or it could be array of string[]

Can we use Linq to xml? How?

spender
  • 117,338
  • 33
  • 229
  • 351
Hemant Kothiyal
  • 4,092
  • 19
  • 61
  • 80
  • 2
    Yes use Linq. There are 10000000+ tutorials about Linq. Maybe you could start with this: http://www.codeproject.com/Articles/19154/Understanding-LINQ-C – sabisabi Jun 27 '12 at 11:23
  • possible duplicate of [LINQ to read XML](http://stackoverflow.com/questions/670563/linq-to-read-xml) – John Saunders Jun 27 '12 at 20:34

6 Answers6

11
var xdoc = XDocument.Load(PATH_TO_FILE);
var companies = xdoc.Descendants("Company").Select(c => (string)c).ToArray();

This will give you a string[].

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
6

Use LINQ to XML, Include using System.Xml.Linq;

   XDocument xmlDoc = XDocument.Load("yourfile.xml");
   var test = xmlDoc.Descendants("Companies").Elements("Company").Select(r => r.Value).ToArray();
   string result = string.Join(",", test);

Output would be:

123,456,789

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    You can just use `.Descendants("Company")` instead of `.Descendants("Companies").Elements("Company")` – Chuck Savage Jun 27 '12 at 14:03
  • 1
    @ChuckSavage, I just wanted to give an idea, and the above statement also works where you want to avoid nested company element such that: 123 child company – Habib Jun 28 '12 at 04:53
  • Yep - I was in a critical place yesterday - sorry for that. – Chuck Savage Jun 28 '12 at 16:47
4

In dataset you can read xml file

Following are lines of code to read XML file in DataSet

DataSet dsMenu = new DataSet(); //Create Dataset Object

dsMenu.ReadXml("XMLFILENAME.XML"); // Read XML file in Dataset

DataTable dtXMLFILE// Create DatyaTable object

dtXMLFILE= dsMenu.Tables[0]; // Store XML Data in Data Table 
Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
3
var xmlStr=@"<MyFile> 
  <Companies> 
    <Company>123</Company> 
    <Company>456</Company>
    <Company>789</Company> 
  </Companies> 
</MyFile>";

var xDoc = XDocument.Parse(xmlStr);
var companyIds = xDoc.Descendants("Company").Select(e => (int)e);
spender
  • 117,338
  • 33
  • 229
  • 351
  • @ChuckSavage Read carefully... they weren't actually explicit about that requirement. Collection of *values* or string array. I fulfil that requirement by the rules of boolean logic. It wouldn't take a genius to subvert this answer to produce strings instead of ints. Hardly worth a downvote... – spender Jun 27 '12 at 15:11
  • OP requested "..." or string[]. If you want the vote back, I can't change the downvote until the answer has been edited. You and I both know, it just takes a `.ToArray()` on the end of your statement to make it an array, but that is what the OP requested. As is, your answer is an `IEnumerable` which isn't the same as a `string[]`. – Chuck Savage Jun 27 '12 at 15:17
3
string pathToXmlFile = @"C:\test.xml";
XElement patternDoc = XElement.Load(pathToXmlFile);
List<string> values = new List<string>();
foreach (var element in patternDoc.Elements("Companies").Elements("Company"))
{
   values.Add(element.Value);
}
thezar
  • 1,278
  • 13
  • 17
2

In the past, I have used an XmlReader and had no difficulties.

MSDN Documentation: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=vs.110).aspx

It is very straightforward and the documentation is pretty well written. A quick demonstration of how to use it:

XmlReader reader = XmlReader.Create(targetFile);

while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element:
            if (reader.Name.Equals("Company")
            {
                // Read the XML Node's attributes and add to string
            }
            break;
    }
}
Keplah
  • 954
  • 2
  • 13
  • 26