Hey Guys,

Quick n00b question...

i have the following XML:

<row>
<UDPName>10014535-Test</UDPName>
<SpeedDialIndex>2</SpeedDialIndex>
<Label>mobile</Label>
<SpeedDialNumber>907********</SpeedDialNumber>
</row>
−
<row>
<UDPName>10014535-Test</UDPName>
<SpeedDialIndex>1</SpeedDialIndex>
<Label>talking clock</Label>
<SpeedDialNumber>9123</SpeedDialNumber>
</row>
-
<row>
<UDPName>10014573-Test</UDPName>
<SpeedDialIndex>1</SpeedDialIndex>
<Label>Work</Label>
<SpeedDialNumber>90203********</SpeedDialNumber>
</row>

(this is a cut down version of the XML.. there would be several hundred rows in the actual xml)

what i need to do is combine these xml bits into a comma seperated string like this:

10014535-Test,1,talking clock,9123,2,mobile,907********
10014573-Test,1,Work,90203*******

i.e. strip out all the tags, and combine any 2 elements with the same <UDPName> value... so that they're on one line with the UDPName showing once at the beginning.

whats the best way to approach this?

Dan

I would suggest LINQ to XML.

You can easily load the XML file into a IEnumerable<> of either a defined or anonymous type and then combine your elements however necessary for your output.

Given the following file format:

<?xml version="1.0" encoding="utf-8" ?>
<rows>
  <row>
    <UDPName>10014535-Test</UDPName>
    <SpeedDialIndex>2</SpeedDialIndex>
    <Label>mobile</Label>
    <SpeedDialNumber>907********</SpeedDialNumber>
  </row>
  <row>
    <UDPName>10014535-Test</UDPName>
    <SpeedDialIndex>1</SpeedDialIndex>
    <Label>talking clock</Label>
    <SpeedDialNumber>9123</SpeedDialNumber>
  </row>
  <row>
    <UDPName>10014573-Test</UDPName>
    <SpeedDialIndex>1</SpeedDialIndex>
    <Label>Work</Label>
    <SpeedDialNumber>90203********</SpeedDialNumber>
  </row>
</rows>

Here is a demo of LINQ to XML. You would then expand upon the code to implement putting your comma-delimited strings together.

using System;
using System.Linq;
using System.Xml.Linq;

namespace LinqToXmlDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlFile = @"C:\Users\Anthony\Documents\Visual Studio 2008\Projects\LinqToXmlDemo\LinqToXmlDemo\demo.xml";

            XDocument document = XDocument.Load(xmlFile);

            var rows = from row in document.Descendants("row")
                       select new
                       {
                           UDPName = row.Element("UDPName").Value,
                           SpeedDialIndex = int.Parse(row.Element("SpeedDialIndex").Value),
                           Label = row.Element("Label").Value,
                           SpeedDialNumber = row.Element("SpeedDialNumber").Value
                       };

            foreach (var row in rows)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", row.UDPName, row.SpeedDialIndex, row.Label, row.SpeedDialNumber);
            }

            Console.Read();
        }
    }
}
Comments
Agree.

Hey Apegram, that looks pretty simple...

im having some issues with references...

i've moved my program to 3.5 as i believe it needs to be to use Linq...

i've added system.core as a reference and now "Using System.linq" is ok...

but using System.xml.linq isnt working...

"The type or namespace name 'Linq' does not exist in the namespace 'System.xml' (are you missing an assembly reference?)"

This question has already been answered. Start a new discussion instead.