1

I have the following XML:

   <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="http://x.table.core.windows.net/" 
      xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
      xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
      xmlns="http://www.w3.org/2005/Atom">
    <title type="text">TestContents</title>
    <updated />
    <link rel="self" title="TestContents" href="TestContents" />

    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit"  />
        <content type="application/xml">
            <m:properties>
                <d:Title>ssq</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit"  />
        <content type="application/xml">
            <m:properties>
                <d:Title>yy</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
    <entry>
        <title type="text" />
        <updated />
        <author>
            <name />
        </author>
        <link rel="edit" />
        <content type="application/xml">
            <m:properties>
                <d:Title>xx</d:Title>
                <d:Type>1</d:Type>
            </m:properties>
        </content>
    </entry>
</feed>

I would like to access the values of <d:Title>yy</d:Title>

Here is the code suggested to me by Darin:

    static void Main(string[] args) {

        XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
        using (var reader = XmlReader.Create(@"c:\data\contents2.xml"))
        {
            var feed = SyndicationFeed.Load(reader);
            foreach (var item in feed.Items)
            {
                var content = item.Content as XmlSyndicationContent;
                if (content != null)
                {
                    var value = content.ReadContent<XElement>();
                    var text = value.Element(d + "Title");
                    Console.WriteLine(text.Value);
                }
            }
            Console.ReadLine();
        }
    }

But when I run this it just gives me an error

Error in line 5 position 3. An error was encountered when parsing a DateTime value in the XML.

1 Answer 1

3

Looks like you are attempting to a parse a syndication feed. I would recommend you using the appropriate tool for that -> SyndicationFeed:

XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
using (var reader = XmlReader.Create("test.xml"))
{
    var feed = SyndicationFeed.Load(reader);
    foreach (var item in feed.Items)
    {
        var content = item.Content as XmlSyndicationContent;
        if (content != null)
        {
            var value = content.ReadContent<XElement>();
            var text = value.Element(d + "Title");
            Console.WriteLine(text.Value);
        }
    }
}

Notice how you need to use the appropriate XNamespace in order to select the custom <d:Title> element.

If you don't want to use the SyndicationFeed class you could parse the XML manually using Linq to XML:

XNamespace a = "http://www.w3.org/2005/Atom";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XElement feed = XElement.Load("test.xml");
var titles =
    from entry in feed.Descendants(a + "entry")
    let content = entry.Element(a + "content")
    let properties = content.Element(m + "properties")
    let title = properties.Element(d + "Title")
    select title;

foreach (var title in titles)
{
    Console.WriteLine(title.Value);            
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I used this code and updated the question. But I still get an error saying "Error in line 5 position 3. An error was encountered when parsing a DateTime value in the XML." I also updated the question with my exact xml.

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.