2

I don't often use Linq to query XML, and I don't have a great deal of experience with XML. What I would like to do is to query this simple XML document...

<AlarmParameters>
    <Parameter>
        <ParameterName>ConsecutivePoints</ParameterName>
        <Points>30</Points>
        <AllowEdit>true</AllowEdit>
        <Caption>Consecutive Points</Caption>
    </Parameter>

    <Parameter>
        <ParameterName>SigmaCount</ParameterName>
        <Count>1</Count>
        <AllowEdit>true</AllowEdit>
        <Caption>Number of Sigmas</Caption>
    </Parameter>
</AlarmParameters>

... And produce a list of 'Parameter' classes, each containing the properties shown in the XML sample. Using LinqPad, I've managed to get this far, but don't really know how to complete this.

string xmlFragment = "<AlarmParameters><Parameter><ParameterName>ConsecutivePoints</ParameterName><Points>30</Points><AllowEdit>true</AllowEdit><Caption>Consecutive Points</Caption></Parameter><Parameter><ParameterName>SigmaCount</ParameterName><Count>1</Count><AllowEdit>true</AllowEdit><Caption>Number of Sigmas</Caption></Parameter></AlarmParameters>";

StringReader strReader = new StringReader(xmlFragment);

XDocument xmlDoc = XDocument.Load(strReader);

var result = from parameter in xmlDoc (not sure what Linq to put here next)
0

1 Answer 1

3

You can find some good information looking at the LINQ to XML overview on MSDN. Here's one way it could be done using the Descendants operator to pull out a collection of elements, and the Element operator to pull out the subproperties of each item in the collection:

var result =
    from parameter in xmlDoc.Descendants("Parameter")
    select new {
        ParameterName = (string) parameter.Element("ParameterName"),
        Points = (int?) parameter.Element("Points") ?? 0,
        Count = (int?) parameter.Element("Count") ?? 0,
        AllowEdit = (bool) parameter.Element("AllowEdit"),
        Caption = (string) parameter.Element("Caption")
    };

This just creates anonymous class instances, but you could replace with select new MyParameterClassName for your parameter class.

Sign up to request clarification or add additional context in comments.

3 Comments

This is almost perfect. I noticed when I execute this query, the class property values contain the value embedded in the element name (e.g., <Points>30</Points>. Any way to get rid of the element name and just get the value?
Ah. Yes, you can use Element("..").Value.
Also digging in a little deeper, it looks like there are built-in explicit operators so if you cast to the type you want, it will automatically cast and handle null references. See the updated example where I added in Count as well. Here's a good reference I got that from: hanselman.com/blog/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.