1

I have by example following XML:

string xml =
@"<plist version='1.0'>
    <dict>
        <key>DataType</key>
        <string>Employee</string>
        <key>8000</key>
        <dict>
            <key>Id</key>
            <string>8000</string>
            <key>Prename</key>
            <string>Walter</string>
            <key>Name</key>
            <string>Walter Lohner Stans</string>
            <key>Initials</key>
            <string>MAL</string>
        </dict>
        <key>8001</key>
        <dict>
            <key>Id</key>
            <string>8001</string>
            <key>Prename</key>
            <string>Motorrad</string>
            <key>Name</key>
            <string> Meierskappel</string>
            <key>Initials</key>
            <string>MAM</string>
        </dict>
        <key>8004</key>
        <dict>
            <key>Id</key>
            <string>8004</string>
            <key>Prename</key>
            <string>Hanspeter</string>
            <key>Name</key>
            <string>Altenbürger AG  Horgen</string>
            <key>Initials</key>
            <string>FH</string>
        </dict>
    </dict>
</plist>";

I would like to get these 3 Employees as a List...

The Class Employee is also defined:

//Class
public class Employee
{
    //Properties    
    public string Id { get; set; }
    public string Prename { get; set; }
    public string Name { get; set; }
    public string Initials { get; set; }
}

How does the parsing work now, if I by example want now to pass the xml to a method and want to have a List (of Type Employees) with these 3 Employees?

I started by doing something like that:

public List<Employee> GetEmployees(string xml)
{
    using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
    {

    }
}

But Actually I don't know how to iterate over all these "dicts" which describe one Employee and generally how to deal with.. Help would be appreciated.

eMi
  • 5,540
  • 10
  • 60
  • 109

4 Answers4

2

It sounds a little like you could benefit from XmlSerializer by using the Deserialize() method.

Here are a few links showing an example:

C# - How to xml deserialize object itself?

C# Deserialize XML to object

I think that is a possible way to go, otherwise you could parse it into XDocument or XmlDocument and navigate through the document with XPath. By the way you can use the Load() method on XmlDocument to load a string of XML

    string xmlString = "<root><sub></sub></root>";
    XmlDocument doc = new XmlDocument();
    doc.Load(xmlString);

or the Parse() method of XDocument to load a string of XML into the object.

string str =
    @"<Root>
        <Child>Content</Child>
    </Root>";
    XDocument doc = XDocument.Parse(str);

Unsure which technique to use, take a look here:

XDocument or XmlDocument

I even managed to find an example from Google that will get you started: http://www.codeproject.com/Articles/169598/Parse-XML-Documents-by-XMLDocument-and-XDocument

Another route is to use the Linq2Xml technique and again here is another question which will show you how it is achieved:

Reading Xml with XmlReader in C#

Here is the blog post that answer was based upon:

http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx

Community
  • 1
  • 1
Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42
  • thank you! very informative and it makes this question even more interesting as I can use it later as a reference! I solved it now with @Anirudha's method: LINQ2XML, very nice solution – eMi Oct 02 '12 at 09:59
1

I Suggest you something like this would work for you.

string xmlpath = "D:\new\test.xml";   

XmlDocument doc = new XmlDocument();
doc.Load(xmlpath);
XmlElement root = doc.DocumentElement;
XmlNodeList employ = root.GetElementsByTagName("Employee");
list<employee> employees=new <employee>();

foreach (XmlElement emp in employ)
{
    string id = emp.GetAttribute("id");
    string name = emp.GetAttribute("name");
    string desc = emp.GetAttribute("Prename");

    Employee e=new employee();
    e.id=id;
    e.Prename =desc;
    e.Name=name;
   employees.add(e);
}
DevT
  • 4,843
  • 16
  • 59
  • 92
  • thx for the answer, but there are 2 things I don't like. The first: I have the XML as string, not as a Document, so I can't use XMLDocument Class. The second: Your not iterating through all Employees, it seems, that ur getting only the first one. – eMi Oct 02 '12 at 09:37
1

Use LINQ2XML..It's COOL

XElement doc=XElement.Parse(".......");

var yourList=doc.Descendants("dict").Descendants("dict").Select(
x=>new Employee
{
Id=x.Elements("string").ElementAt(0).Value,
Prename=x.Elements("string").ElementAt(1).Value,
Name=x.Elements("string").ElementAt(2).Value,
Initials=x.Elements("string").ElementAt(3).Value
}
);
Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • Thx for the answer, nice way, didn't know about! There are 2 things u should change, use: `XElement doc = XElement.Parse(xml);` (I have no file) and the second, in Elements use string, not key – eMi Oct 02 '12 at 09:57
  • "yourList" must be var, u cant do List, it gives an error. Cast is also not possible. that was the reason I edited your code, cause its not working so, like it is – eMi Oct 02 '12 at 10:10
0

Try to use something like this:

XmlDocument x=new XmlDocument();
x.LoadXml(xml);

foreach (XmlNode x1 in x.SelectNodes("//dict"))
{
    string Id=x1.SelectSingleNode("dict[key[text()='Id']]/string").InnerText;
}
Jake1164
  • 12,291
  • 6
  • 47
  • 64
MikkaRin
  • 3,026
  • 19
  • 34