【问题标题】:How to get certain elements inside an XML document如何获取 XML 文档中的某些元素
【发布时间】:2017-03-16 16:25:45
【问题描述】:

我正在像这样查询我的 SP 列表:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myspsite/_api/web/lists/getByTitle('the%20title')/items");

request.Method = "GET";
request.ContentType = "text/xml";
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

XDocument xd = XDocument.Load(readStream);
response.Close();
readStream.Close();

这给了我以下信息:

<feed xml:base="http://myspsite/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
  <id>090ds-211312-asdadsasd</id>
  <title />
  <updated>2011-03-16T15:33:25Z</updated>
  <entry m:etag="&quot;7&quot;">
    <id>a4846531-dae6-413c-bbbe-b3012342ewerred0d1</id>
    <category term="SP.Data.My.ListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" href="Web/Lists(guid'0eef-a924-471e-80f0-8d71erwsdf3d6d')/Items(10)" />
    <title />
    <updated>2012-03-16T15:33:25Z</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">38</d:Id>
        <d:ContentTypeId>0x010400C64826486CBEE74BBFF0werwerewsdfD</d:ContentTypeId>
        <d:Title>This is my Title</d:Title>
        <d:Body>This is the body</d:Body>
        <d:Summaries>This is the body summary</d:Summaries>
        <d:Include m:type="Edm.Boolean">false</d:Include>
        <d:Add_Date m:type="Edm.DateTime">2015-10-07T04:00:00Z</d:Add_Date>
        <d:ID m:type="Edm.Int32">38</d:ID>
        <d:OData__UIVersionString>1.0</d:OData__UIVersionString>
        <d:GUID m:type="Edm.Guid">69504c8e-6897-49e6-926d-d54c900524e5</d:GUID>
      </m:properties>
    </content>
  </entry>
  <entry m:etag="&quot;7&quot;">
    <id>a4846531-dae6-413c-bbbe-b301234234ewd0d1</id>
    <category term="SP.Data.My.ListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" href="Web/Lists(guid'0ee1d7ef-a924-471e-80f0-8d71erwsdf3d6d')/Items(38)" />
    <title />
    <updated>2012-03-16T15:33:25Z</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">38</d:Id>
        <d:ContentTypeId>0x010400C64826486CBEE7sdfsfdsdf3D</d:ContentTypeId>
        <d:Title>This is my Title</d:Title>
        <d:Body>This is the body</d:Body>
        <d:Summaries>This is the body summary</d:Summaries>
        <d:Include m:type="Edm.Boolean">false</d:Include>
        <d:Add_Date m:type="Edm.DateTime">2015-10-07T04:00:00Z</d:Add_Date>
        <d:ID m:type="Edm.Int32">38</d:ID>
        <d:OData__UIVersionString>1.0</d:OData__UIVersionString>
        <d:GUID m:type="Edm.Guid">69504c8e-6897-49e6-926d-d54c900524e5</d:GUID>
      </m:properties>
    </content>
  </entry>
  //... more entry
</feed>

如何获取每个entryd:Titled:Body

我尝试使用:

foreach (XElement element in xd.Descendants("entry"))
{
  Console.WriteLine(element);
}

但这没有用。

请帮我获取entry中的节点。

【问题讨论】:

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


    【解决方案1】:

    您应该使用 XName 而不是标签名称。

     foreach (XElement element in xd.Descendants("{http://www.w3.org/2005/Atom}entry"))
                {
                  Console.WriteLine(element);
                }
    

    //读取d:title和d:body,

    XName title = XName.Get("Title", "http://schemas.microsoft.com/ado/2007/08/dataservices");
                XName body = XName.Get("Body", "http://schemas.microsoft.com/ado/2007/08/dataservices");
                foreach (XElement element in xd.Descendants("{http://www.w3.org/2005/Atom}entry"))
                {
                    //Iterate the child element [ element.Descendants(title)], to get the title and body
                    //To simply the code, I used FirstOrDefault here
                    Console.WriteLine(element.Descendants(title).FirstOrDefault().Value);
                    Console.WriteLine(element.Descendants(body).FirstOrDefault().Value);               
    
                }
    

    这对我有用。

    【讨论】:

    • 我也是...谢谢...我如何获得d:Titled:Body,我可以将元素视为常规XML 节点吗?
    • 我正在尝试,但希望你应该使用 XName。
    • 我也是...谢谢。
    • 谢谢。我 +1 你的答案。
    • 如果可能,请您帮忙:stackoverflow.com/questions/42907980/… 谢谢。
    【解决方案2】:
        XName properties = XName.Get("properties", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
        XName title = XName.Get("Title", "http://schemas.microsoft.com/ado/2007/08/dataservices");
        XName body = XName.Get("Body", "http://schemas.microsoft.com/ado/2007/08/dataservices");
        var result = xd.Descendants(properties).Select(en => new { title = en.Element(title), body = en.Element(body) });
    

    【讨论】:

    • 不。它不工作。它显示为Enumeration yielded no results
    • 可悲的运行时错误。 An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll Additional information: The ':' character, hexadecimal value 0x3A, cannot be included in a name.
    • @CPR43 谢谢。这对我有用......太棒了!
    • 是的..如果您能接受这个答案,那就太好了
    • xd.Descendants(properties).Where(p=>p.Element(title).value == "testing1").Select(p=>p);
    【解决方案3】:

    您在这里拥有的不是标准的 xml,而是原子提要。 试试这个:ASP.net c#. How do I parse an atom feed from a blog 或者谷歌一个框架

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 2012-10-31
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      相关资源
      最近更新 更多