1

I have the xml code:

<feed xml:base="https://test.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">CustomTable5</title>
    <id>https://eqmetest.table.core.windows.net/CustomTable5</id>
    <updated>2011-11-15T11:36:32Z</updated>
    <link rel="self" title="CustomTable5" href="CustomTable5" />
    <entry m:etag="W/&quot;datetime'2011-11-05T08%3A29%3A45.8347866Z'&quot;">
        <id>https://eqmetest.table.core.windows.net/CustomTable5(PartitionKey='',RowKey='1')</id>
        <title type="text"></title>
        <updated>2011-11-15T11:36:32Z</updated>
        <author>
            <name />
        </author>
        <link rel="edit" title="CustomTable5" href="CustomTable5(PartitionKey='',RowKey='1')" />
        <category term="eqmetest.CustomTable5" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey></d:PartitionKey>
                <d:RowKey>1</d:RowKey>
                <d:Timestamp m:type="Edm.DateTime">2011-11-05T08:29:45.8347866Z</d:Timestamp>
                <d:LastID m:type="Edm.Int32">54</d:LastID>
            </m:properties>
        </content>
    </entry>      
</feed>

I need to have a list of Name of elements (in my case it should be:PartitionKey,RowKey,Timestamp,LastID ) and if there will be much more tags my list should be from the one where LastID.Value==someId

Just to start I have wrote this:

 XmlNamespaceManager xs = new XmlNamespaceManager(new NameTable());
            xs.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
            xs.AddNamespace("a", "http://www.w3.org/2005/Atom");
            xs.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");

            var propertiesElement = document.XPathSelectElements("/a:feed/a:entry/a:content/m:properties", xs);

But I dont know how to end)

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
revolutionkpi
  • 2,632
  • 10
  • 45
  • 84

2 Answers2

2
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace atom = "http://www.w3.org/2005/Atom";

int id = 54;

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

List<XElement> properties = doc.Element(atom + "feed")
                        .Elements(atom + "entry")
                        .Elements(atom + "content")
                        .Elements(m + "properties")
                        .ToList();

XElement propertiesEl = properties.Count() > 1 ? properties.FirstOrDefault(p => (int)p.Element(m + "LastID") == id) : properties[0];

List<string> names = propertiesEl.Elements().Select(el => el.Name.LocalName).ToList();
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
1

Try this, it should work when the element is there or not, and grabs just the first one found that matches the search value:

    public static void Main()
    {
        var searchValue = "54";
        var xdoc = XDocument.Load(@"foo.xml");

        XNamespace nsD = "http://schemas.microsoft.com/ado/2007/08/dataservices";
        XNamespace nsM = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
        XNamespace nsDefault = "http://www.w3.org/2005/Atom";

        var foundElem = xdoc.Root
            .Elements(nsDefault + "entry")
            .Elements(nsDefault + "content")
            .Elements(nsM + "properties")
            .Elements(nsD + "LastID")
            .Where(e => e.Value == searchValue)
            .FirstOrDefault();

        if (foundElem != null)
        {
            var siblingElems = foundElem.Parent.Elements();
            foreach (var elem in siblingElems)
            {
                Console.WriteLine("{0}", elem.Name.LocalName);
            }
        }
    }

And the output is:

PartitionKey
RowKey
Timestamp
LastID
JohnD
  • 14,327
  • 4
  • 40
  • 53
  • as I have wrote It should return a list of Name of child elements of so in my case list should be like this: PartitionKey,RowKey,Timestamp,LastID – revolutionkpi Nov 15 '11 at 12:32
  • I updated it to show how to get that output. You can tweak the code as needed... – JohnD Nov 15 '11 at 12:46