使用XDocument和linq解析xml

时间:2018-10-31 12:14:57

标签: asp.net linq

我有以下来自sharepoint的xml

<?xml version="1.0" encoding="utf-8" ?>
- <feed xml:base="https://meeting-3/_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>704abed5-c027-4d58-8152-1df3cd763ee1</id>
  <title />
  <updated>2018-10-31T10:17:37Z</updated>
  - <entry m:etag="5">
    <id>Web/Lists(guid'3a761324-3731-4aed-9fcd-4c89d59ace40')/Items(1)</id>
    <category term="SP.Data.Shared_x0020_DocumentsItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <link rel="edit" href="Web/Lists(guid'3a761324-3731-4aed-9fcd-4c89d59ace40')/Items(1)" />
    <title />
    <updated>2018-10-31T10:17:37Z</updated>
    - <author>
      <name />
    </author>
    - <content type="application/xml">
      - <m:properties>
        <d:Id m:type="Edm.Int32">1</d:Id>
        <d:ID m:type="Edm.Int32">1</d:ID>
        <d:Created m:type="Edm.DateTime">2018-09-25T14:08:43Z</d:Created>
        <d:Modified m:type="Edm.DateTime">2018-10-26T14:03:57Z</d:Modified>
        <d:FileRef>/GMM/meeting-3/Shared Documents/Monthly News August 2018.pdf</d:FileRef>
        <d:Modified_x0020_By>i:0#.w|62698</d:Modified_x0020_By>
        <d:Created_x0020_By>i:0#.w|58314</d:Created_x0020_By>
        <d:Title>XYZ</d:Title>
        - <d:Meeting_x0020_Doc_x0020_Type m:type="SP.Taxonomy.TaxonomyFieldValue">
          <d:Label>3</d:Label>
          <d:TermGuid>338af5a7-52dd-44e4-a22a-651a6f7b6d8b</d:TermGuid>
          <d:WssId m:type="Edm.Int32">3</d:WssId>
        </d:Meeting_x0020_Doc_x0020_Type>
        <d:Publish_x0020_Status>Published</d:Publish_x0020_Status>
      </m:properties>
    </content>
  </entry>
</feed>

如果我使用此代码,则该

using (XmlReader reader = XmlReader.Create(siteUrl, settings))
{
    while (reader.Read())
    {
    if (reader.IsStartElement())
    {
        switch (reader.Name)
        {
        case "feed":
            System.Web.HttpContext.Current.Response.Write("Start <perls> element.<br />");
            break;
        case "entry":

是否为我拉回一些数据。但是如您所见,xml文档中有许多子项,然后有许多子项。并且会有很多“ entry”标签

所以我正在使用

siteUrl = "https://urlofsharepoint";
var securePassword = new SecureString();
foreach (char c in "mypassword")
{
    securePassword.AppendChar(c);
}

XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = new NetworkCredential("myusername", securePassword, "group");
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;

XmlReader reader = XmlReader.Create(siteUrl, settings);
reader.MoveToContent();
XDocument xdoc = XDocument.ReadFrom(reader);
Response.Write(xdoc);

xdoc.Descendants("entry").Select(p => new
{
    id = p.Attribute(" m:etag").Value
}).ToList().ForEach(p =>
{
    Response.Write("id " + p.id + "<br />");
});

但是由于某些原因,除了“ ff2fd5a1-86fc-4c45-be9f-52cd627a2214”之外,什么都没有返回

我不知道这是什么意思。

有人可以让我知道我在做什么错吗?

0 个答案:

没有答案