XML C#Parse - 复杂

时间:2017-12-06 00:22:59

标签: c# xml

我已经在谷歌上呆了一段时间,但我只是难倒了。我需要解析这种性质的xml。我似乎无法跳到中间的元素,例如夹。我限制了xml,因为有很多'文件夹'元素。任何指导将不胜感激。我是在FolderID元素的属性ID之后。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <h:ServerVersionInfo MajorVersion="14" 
        MinorVersion="1" 
        MajorBuildNumber="225" 
        MinorBuildNumber="46" 
        Version="Exchange2010_SP1" 
        xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
        xmlns="http://schemas.microsoft.com/exchange/services/2006/types" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:GetFolderResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
        xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
      <m:ResponseMessages>
        <m:GetFolderResponseMessage ResponseClass="Success">
          <m:ResponseCode>NoError</m:ResponseCode>
          <m:Folders>
            <t:Folder>
              <t:FolderId Id="AAMkADk5MmY1ZThmLTM2MzAtNGVh" ChangeKey="AQAAABYAAACSe/NBrSZiQKqHx8yL+lIRAAAA1EWM" />
              <t:ParentFolderId Id="AAMkADk5MmY1ZThmLTM2MzAtNGVh" ChangeKey="AQAAAA==" />
              <t:DisplayName>Top of Information Store</t:DisplayName>
              <t:TotalCount>0</t:TotalCount>
              <t:ChildFolderCount>15</t:ChildFolderCount>
              <t:EffectiveRights>
                <t:CreateAssociated>true</t:CreateAssociated>
                <t:CreateContents>true</t:CreateContents>
                <t:CreateHierarchy>true</t:CreateHierarchy>
                <t:Delete>true</t:Delete>
                <t:Modify>true</t:Modify>
                <t:Read>true</t:Read>
                <t:ViewPrivateItems>true</t:ViewPrivateItems>
              </t:EffectiveRights>
              <t:PermissionSet>
                <t:Permissions>
                  <t:Permission>
                    <t:UserId>
                      <t:DistinguishedUser>Default</t:DistinguishedUser>
                    </t:UserId>
                    <t:CanCreateItems>false</t:CanCreateItems>
                    <t:CanCreateSubFolders>false</t:CanCreateSubFolders>
                    <t:IsFolderOwner>false</t:IsFolderOwner>
                    <t:IsFolderVisible>false</t:IsFolderVisible>
                    <t:IsFolderContact>false</t:IsFolderContact>
                    <t:EditItems>None</t:EditItems>
                    <t:DeleteItems>None</t:DeleteItems>
                    <t:ReadItems>None</t:ReadItems>
                    <t:PermissionLevel>None</t:PermissionLevel>
                  </t:Permission>
                  <t:Permission>
                    <t:UserId>
                      <t:DistinguishedUser>Anonymous</t:DistinguishedUser>
                    </t:UserId>
                    <t:CanCreateItems>false</t:CanCreateItems>
                    <t:CanCreateSubFolders>false</t:CanCreateSubFolders>
                    <t:IsFolderOwner>false</t:IsFolderOwner>
                    <t:IsFolderVisible>false</t:IsFolderVisible>
                    <t:IsFolderContact>false</t:IsFolderContact>
                    <t:EditItems>None</t:EditItems>
                    <t:DeleteItems>None</t:DeleteItems>
                    <t:ReadItems>None</t:ReadItems>
                    <t:PermissionLevel>None</t:PermissionLevel>
                  </t:Permission>
                </t:Permissions>
              </t:PermissionSet>
              <t:UnreadCount>0</t:UnreadCount>
            </t:Folder>
          </m:Folders>
        </m:GetFolderResponseMessage>
      </m:ResponseMessages>
    </m:GetFolderResponse>
  </s:Body>
</s:Envelope> 

4 个答案:

答案 0 :(得分:0)

你应该学习血清化。在C#中将XML转换为对象非常容易。 https://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part

那说,这将为您提供您所追求的数据。它不是非常可重复使用,并且不会帮助您使用除具有该属性的一个实例的xml文件之外的任何xml文件,但这就是您所追求的内容。

string FolderId;
string ChangeKey;
using (StreamReader sr = new StreamReader("c:\\myfile.xml"))
{
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        if (line.Contains("<t:FolderId Id="))
        {
            try
            {
                var lineArray = line.Split('\"');
                FolderId = lineArray[1];
                ChangeKey = lineArray[3];
            }
            catch
            {
                // handle exception
            }               
        }
    }
}

答案 1 :(得分:0)

您可以使用XSD.exe创建架构类。然后使用XML反序列化器,您可以反序列化/解析xml到对象

答案 2 :(得分:0)

LINQ to XML是最好的.NET XML Parsing API。 看到 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/xdocument-class-overview

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ConsoleApp12
{
    class Program
    {
        static void Main(string[] args)
        {

            var doc = XDocument.Load(@"c:\temp\foo.xml");
            var ns = (XNamespace)"http://schemas.microsoft.com/exchange/services/2006/types";

            var folders = doc.Descendants(ns + "Folder");

            foreach (var e in folders)
            {
                var folderId = e.Element(ns + "FolderId").Attribute("Id").Value;
                Console.WriteLine(folderId);
            }

            Console.WriteLine("Hit any key to exit.");
            Console.ReadKey();

        }
    }
}

答案 3 :(得分:-1)

使用xml linq

            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> folders = doc.Descendants().Where(x => x.Name.LocalName == "Folder").ToList();
            XNamespace tNs = folders.FirstOrDefault().GetNamespaceOfPrefix("t");
            XElement id_AAMkADk5MmY1ZThmLTM2MzAtNGVh = folders.Where(x => (string)x.Element(tNs + "FolderId").Attribute("Id") == "AAMkADk5MmY1ZThmLTM2MzAtNGVh").FirstOrDefault();