如何通过ATOM提要将多个节点合并为一个 [英] How to combine multiple nodes into one from a ATOM feed

查看:56
本文介绍了如何通过ATOM提要将多个节点合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

theResult XDocument:

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>

代码:

...
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给出以下内容:

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>}}

如您所见,它将每个entry分为两个单独的索引.

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

如何做到这一点,使其遍历每个主entry节点(<entry m:etag="&amp;quot;1&amp;quot;">),并从<m:inline>节点给我TheUrl,并在每个<m:properties>节点给我Title, Tag, FullJourn索引?

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?

因此result2将成为每个entry的索引,如下所示:

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>}}

推荐答案

尝试一下

       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()

        });

这篇关于如何通过ATOM提要将多个节点合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆