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?

Dawid
  • 385
  • 9
  • 19
  • 2
    LINQ to XML is a good choice. You should try it. – MarcinJuraszek Mar 29 '14 at 03: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#. – Aron Mar 29 '14 at 03:58
  • Please clarify what data you're trying to select. The [XElement class](http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx) is the preferred way to use LINQ and XML in C# these days. –  Mar 29 '14 at 08: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). – Dawid Mar 29 '14 at 09:29
  • @Aron you mean deserialize it using XMLFormatter and Stream ? – Dawid Mar 29 '14 at 09:30
  • I mean either you use `DataContractSerializer` or `XmlSerializer`. – Aron Mar 29 '14 at 09:32

2 Answers2

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;
  • this gives me `System.NullReferenceException` and the `title.Attribute("type").Value == "official"` is highlighted... – Dawid Mar 29 '14 at 21:19
  • 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... –  Mar 29 '14 at 21:47
  • 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. –  Mar 29 '14 at 21:51
  • OK, my bad. I've studied the whole xml file and found two more `` tags that do not have type attribute but I don't need them. Gonna try with `doc.XPathSelectElements("/titles/title")` – Dawid Mar 29 '14 at 22:27
  • Ok, small fix `doc.XPathSelectElements("//titles/title")` and works perfectly, thanks XD – Dawid Mar 29 '14 at 22:31
  • One more thing: how to handle `xml:lang` attribute ? – Dawid Mar 29 '14 at 23:27
  • Please see [The ':' character, hexadecimal value 0x3A, cannot be included in a name](http://stackoverflow.com/q/2575546/456814), and [“The ':' character, hexadecimal value 0x3A, cannot be included in a name”](http://stackoverflow.com/a/7213490/456814). –  Mar 29 '14 at 23:40
  • Was searching for examples of Xmlserializer and found this: http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document#answer-19613934 So why not to try. I did Ctrl+C and `Edit -> Paste Special -> Paste XML As Classes` in VS 2013 and... Whoa I got all the classes generated. One condition target framework must be 4.5 – Dawid Mar 30 '14 at 01:43
  • If that worked for you, put it as another answer to this question, and you can even accept it in 48 hours if you want. –  Mar 30 '14 at 01:48
  • Good point, other should know too. Anyway thank you for all your help. – Dawid Mar 30 '14 at 01:53
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)

Community
  • 1
  • 1
Dawid
  • 385
  • 9
  • 19