How to combine multiple nodes into one from a ATOM feed

眉间皱痕 提交于 2019-12-12 03:27:41

问题


theResult XDocument:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xml:base="http://mysharepointhomesite/_api/">
    <id>ecfe572e-051e-4d49-899e-1c025wersfd7d</id>
    <title />
    <updated>2017-03-20T14:13:12Z</updated>
    <entry m:etag="&amp;quot;1&amp;quot;">
        <id>b9140dc1-8af9-4fff-8a2f-ea234sdc81cab9</id>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AttachmentFiles" type="application/atom+xml;type=feed" title="AttachmentFiles" href="Web/Lists(guid'567ecd47-26d2-42fd-b9e0-7793456fef693fa')/Items(1)/AttachmentFiles">
            <m:inline>
                <feed>
                    <id>558daaf0-5737-4c60-aada-e6ead10b98b4</id>
                    <title />
                    <updated>2017-03-20T14:13:12Z</updated>
                    <entry>
                        <id>http://mysharepointhomesite/_api/Web/Lists(guid'567ec345345d47-26d2-42fd-b9e0-779asd234234')/Items(1)/AttachmentFiles('animation-1.gif')</id>
                        <category term="SP.Attachment" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                        <link rel="edit" href="Web/Lists(guid'567ecd47-26d2-42345345fd-b9e0-7796fef693fa')/Items(1)/AttachmentFiles('animation-1.gif')" />
                        <title />
                        <updated>2017-03-20T14:13:12Z</updated>
                        <author>
                            <name />
                        </author>
                        <content type="application/xml">
                            <m:properties>
                                <d:FileName>animation-1.gif</d:FileName>
                                <d:TheUrl>/Lists/ListText/Attachments/1/animation-1.gif</d:TheUrl>
                            </m:properties>
                        </content>
                    </entry>
                </feed>
            </m:inline>
        </link>
        <title />
        <updated>2017-03-20T14:13:12Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
                <d:Id m:type="Edm.Int32">1</d:Id>
                <d:ContentTypeId>0x01001331E6E712698844915481D55B123CFB</d:ContentTypeId>
                <d:Title>This is title 1</d:Title>
                <d:Tag>This is Tag1</d:Tag>
                <d:FullJournal m:null="true" />
                <d:ID m:type="Edm.Int32">1</d:ID>
                <d:Modified m:type="Edm.DateTime">2017-03-17T19:29:38Z</d:Modified>
                <d:Created m:type="Edm.DateTime">2017-03-17T19:29:38Z</d:Created>
                <d:AuthorId m:type="Edm.Int32">11</d:AuthorId>
                <d:EditorId m:type="Edm.Int32">11</d:EditorId>
                <d:OData__UIVersionString>1.0</d:OData__UIVersionString>
                <d:Attachments m:type="Edm.Boolean">true</d:Attachments>
                <d:GUID m:type="Edm.Guid">01551424-e6d4-44c5-8539-f70f234sdsdfd7ba</d:GUID>
            </m:properties>
        </content>
    </entry>
    <entry m:etag="&amp;quot;1&amp;quot;">
        <id>d1fdeef1-ba91-4c58-be58-fd73ert03dced7f</id>
        <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/AttachmentFiles" type="application/atom+xml;type=feed" title="AttachmentFiles" href="Web/Lists(guid'567ecd47-26d2-42fertetrd-b9e0-7796fef693fa')/Items(4)/AttachmentFiles">
            <m:inline>
                <feed>
                    <id>235f72bd-8663-4230-9ertet9cf-de97f5234234sdee</id>
                    <title />
                    <updated>2017-03-20T14:13:12Z</updated>
                    <author>
                        <name />
                    </author>
                </feed>
            </m:inline>
        </link>
        <title />
        <updated>2017-03-20T14:13:12Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
                <d:Id m:type="Edm.Int32">4</d:Id>
                <d:ContentTypeId>0x01001331E6E712698844915481D55B123CFB</d:ContentTypeId>
                <d:Title>This is title 2</d:Title>
                <d:Tag>This is Tag2</d:Tag>
                <d:FullJournal>&amp;lt;div class="ExternalClassCB0921E6581F4789A9SDSDF234F2C1BC29E6B"&amp;gt;&amp;lt;p&amp;gt;​This is a sample Article which may have &amp;lt;strong&amp;gt;BOLD&amp;lt;/strong&amp;gt;&amp;lt;/div&amp;gt;</d:FullJournal>
                <d:ID m:type="Edm.Int32">4</d:ID>
                <d:Modified m:type="Edm.DateTime">2017-03-20T14:12:53Z</d:Modified>
                <d:Created m:type="Edm.DateTime">2017-03-20T14:12:53Z</d:Created>
                <d:AuthorId m:type="Edm.Int32">11</d:AuthorId>
                <d:EditorId m:type="Edm.Int32">11</d:EditorId>
                <d:OData__UIVersionString>1.0</d:OData__UIVersionString>
                <d:Attachments m:type="Edm.Boolean">false</d:Attachments>
                <d:GUID m:type="Edm.Guid">956d799f-53sdsdfsdf23-44b3-bbb2-c4643543erta020dc</d:GUID>
            </m:properties>
        </content>
    </entry>
    //..more entry
</feed>

Code:

...
XName entry = XName.Get("entry", "http://www.w3.org/2005/Atom");
XName content = XName.Get("content", "http://www.w3.org/2005/Atom");
XName properties2 = XName.Get("properties", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
XName srl = XName.Get("TheUrl", "http://schemas.microsoft.com/ado/2007/08/dataservices");
XName title2 = XName.Get("Title", "http://schemas.microsoft.com/ado/2007/08/dataservices");
XName tag = XName.Get("Tag", "http://schemas.microsoft.com/ado/2007/08/dataservices");
XName journ = XName.Get("journ", "http://schemas.microsoft.com/ado/2007/08/dataservices");
var result2 = theResult.Descendants(content).Descendants(properties2)
    .Select(en => new {
        srl = en.Element(srl)
        , title2 = en.Element(title2)
        , tag = en.Element(tag)
        , journ = en.Element(journ)
    });
...

result2 gives out the following:

0: { srl = {<d:TheUrl>/Lists/ListText/Attachments/1/animation-1.gif</d:TheUrl>}, title2 = null, tag = null, journ = null}
1: { srl = null, title2 = {<d:Title>This is title 1</d:Title>}, tag = {<d:Tag>This is Tag1</d:Tag>}, journ = null}
2: { srl = null, title2 = {<d:Title>New Attachment</d:Title>}, tag = null, journ = null}
3: { srl = null, title2 = {<d:Title>This is title 2</d:Title>}, tag = {<d:Tag>This is summary tag</d:Tag>}, journ = {<d:FullJournal>&amp;lt;div class="ExternalClassCB0921E6581F4789A9SDSDF234F2C1BC29E6B"&amp;gt;&amp;lt;p&amp;gt;​This is a sample Article which may have &amp;lt;strong&amp;gt;BOLD&amp;lt;/strong&amp;gt;&amp;lt;/div&amp;gt;</d:FullJournal>}}

As you can see it splits each entry into two separate indexes.

How can I make it so that, it goes through every primary entry node (<entry m:etag="&amp;quot;1&amp;quot;">) and gives me TheUrl from the <m:inline> node, and gives me Title, Tag, FullJourn from <m:properties> node per index?

So result2 would become one index per entry like the following:

0: { srl = {<d:TheUrl>/Lists/ListText/Attachments/1/animation-1.gif</d:TheUrl>}, title2 = {<d:Title>This is title 1</d:Title>}, tag = {<d:Tag>This is Tag1</d:Tag>}, journ = null}
2: { srl = null, title2 = {<d:Title>This is title 2</d:Title>}, tag = {<d:Tag>This is Tag2</d:Tag>}, journ = {<d:FullJournal>&amp;lt;div class="ExternalClassCB0921E6581F4789A9SDSDF234F2C1BC29E6B"&amp;gt;&amp;lt;p&amp;gt;​This is a sample Article which may have &amp;lt;strong&amp;gt;BOLD&amp;lt;/strong&amp;gt;&amp;lt;/div&amp;gt;</d:FullJournal>}}

回答1:


Try this

       XName entry = XName.Get("entry", "http://www.w3.org/2005/Atom");
        XName content = XName.Get("content", "http://www.w3.org/2005/Atom");
        XName properties2 = XName.Get("properties", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
        XName srl = XName.Get("TheUrl", "http://schemas.microsoft.com/ado/2007/08/dataservices");
        XName title2 = XName.Get("Title", "http://schemas.microsoft.com/ado/2007/08/dataservices");
        XName tag = XName.Get("Tag", "http://schemas.microsoft.com/ado/2007/08/dataservices");
        XName journ = XName.Get("FullJournal", "http://schemas.microsoft.com/ado/2007/08/dataservices");
        XName etag = XName.Get("etag", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");

        var result2 = theResult.Descendants(entry).Where(en => en.HasAttributes).Select(en => new
        {
            srl = en.Descendants(srl).FirstOrDefault(),

            title2 = en.Descendants(title2).FirstOrDefault()
            ,
            tag = en.Descendants(tag).FirstOrDefault()
            ,
            journ = en.Descendants(journ).FirstOrDefault()

        });


来源:https://stackoverflow.com/questions/42907980/how-to-combine-multiple-nodes-into-one-from-a-atom-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!