0

I have xml files which look like this:

<?xml version="1.0" encoding="utf-8"?>
<record id="177" restricted="false">
    <type>record type</type>
    <startdate>2000-10-10</startdate>
    <enddate>2014-02-01</enddate>
    <titles>
        <title xml:lang="en" type="main">Main title</title>
        <!-- only one title element with type main -->
        <title xml:lang="de" type="official">German title</title>
        <!-- can have more titles of type official -->
    </titles>
    <description>description of the record</description>
    <categories>
        <category id="122">
            <name>category name</name>
            <description>category description</description>
        </category>
        <!-- can have more categories -->
    </categories>
    <tags>
        <tag id="5434">
            <name>tag name</name>
            <description>tag description</description>
        </tag>
        <!-- can have more tags -->
    </tags>
</record>

How do I select the data from these xml files using LINQ, or should I use something else?

6
  • 2
    LINQ to XML is a good choice. You should try it. Commented Mar 29, 2014 at 3:56
  • 1
    Given you know the schema I would suggest you deserialize it to objects first. Then its much easier to work with in C#. Commented Mar 29, 2014 at 3:58
  • Please clarify what data you're trying to select. The XElement class is the preferred way to use LINQ and XML in C# these days. Commented Mar 29, 2014 at 8:10
  • 1
    @Cupcake The file contains some more data but those that are specified here are needed by me (record type, start date, end date, title main and all translations, categories with id, name and description and same with tags). Do you have any examples, I know how to create XDocument and add XElements with attributes and then save it to file but how to do it in the other way (xml string to XElemet and XDocument). Commented Mar 29, 2014 at 9:29
  • @Aron you mean deserialize it using XMLFormatter and Stream ? Commented Mar 29, 2014 at 9:30

2 Answers 2

1

You can load xml into XDocument objects using either the Load() method for files, or the Parse() method for strings:

var doc = XDocument.Load("your-file.xml");
// OR
var doc = XDocument.Parse(yourXmlString);

Then you can access the data using LINQ:

var titles =
    from title in doc.XPathSelectElements("//title")
    where title.Attribute("type").Value == "official"
    select title.Value;
Sign up to request clarification or add additional context in comments.

10 Comments

this gives me System.NullReferenceException and the title.Attribute("type").Value == "official" is highlighted...
It's probably giving you that because the title node has no type attribute, so it throws an exception when trying to call .Value on a null value. You didn't really specify how you wanted to retrieve data, so I just made my own smaller sample xml to work with. You can work around that problem by checking for null before accessing child elements. Wait, hold on, let me double-check...
Okay, I double-checked, I used the exact XML file that you have in your question, and I'm not getting any NullReferenceExceptions. If you're using a different XML file than the one you have above in your question, and you're getting exceptions and can't figure out why or how to fix it, then you should ask another question.
OK, my bad. I've studied the whole xml file and found two more <title> tags that do not have type attribute but I don't need them. Gonna try with doc.XPathSelectElements("/titles/title")
Ok, small fix doc.XPathSelectElements("//titles/title") and works perfectly, thanks XD
|
0

Was searching for examples of Xmlserializer and found this: How to Deserialize XML document So why not to try. I did Ctrl+C and Edit -> Paste Special -> Paste XML As Classes in Visual Studio 2013 and... Whoa I got all the classes generated. One condition target framework must be 4.5 and this function is available from Visual Studio 2012+ (as stated in that post)

Comments

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.