tags:

views:

27

answers:

2

I'm slightly overwhelmed by how many ways there are of processing XML in .NET.. In my case, I'm porting a ton of C lines over and I would like to wrap my code and keep it basically the way I had it (but use classes of course). I'm not exactly looking for THE fastest way, but more or less something consistent with the API I had before.

My specific API is like this: (This is C code)

XML* xml = XML_Load("file.xml");
XML_OBJECT* obj;

XML_FindObjectByTagName(xml->pstFirstObject, "objectname", &obj);

/* Go through objects at that level */
for (obj = obj; obj != null; obj = obj->pstNext)
{
    char* s = XML_ReadAttribute_String(obj, "attribname");

    /* This is how I use my current API, and I would like to sorta keep it similar */
}

So, I'm not sure if I can use one of the .NET classes and keep it similar looking, or if I should make a wrapper. If anyone knows a good solution for me, please let me know. I would really like to find the functions that let me find objects from their name, then be able to do a sorta (foreach) for the result. Thanks!

+3  A: 

I find that LINQ to XML is the best XML API in .NET for most tasks.

To find all the descendant elements with a particular name, you'd use:

XDocument doc = XDocument.Load("file.xml");

foreach (XElement element in doc.Descendants("objectname"))
{
    string s = (string) element.Attribute("attribname");
    // ...
}
Jon Skeet
Brilliant. Thanks
Kyle
+1  A: 

I do not know about C, but .NET has always had XmlDocument which is a DOM representation of XML. For fast processing you can use XmlReader.

Aliostad
I thought I had it wrong, it was long time since I used it.
Aliostad
While you certainly *can* do all of this with `XmlDocument`, LINQ to XML makes many things a lot simpler IMO.
Jon Skeet