如何将多个节点组合成一个ATOM源

时间:2017-03-20 15:44:32

标签: c# sharepoint linq-to-xml sharepoint-api

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拆分为两个单独的索引。

如何制作,以便它通过每个主要entry节点(<entry m:etag="&amp;quot;1&amp;quot;">)并从TheUrl节点向我<m:inline>提供,并告诉我{ {1}}来自每个索引的Title, Tag, FullJourn节点?

因此,<m:properties>将成为每result2个索引,如下所示:

entry

1 个答案:

答案 0 :(得分:1)

试试这个

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

        });