【问题标题】:How to parse xml如何解析xml
【发布时间】:2011-12-29 11:18:03
【问题描述】:

我有xml代码:

<feed xml:base="https://test.net/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
    <title type="text">CustomTable5</title>
    <id>https://eqmetest.table.core.windows.net/CustomTable5</id>
    <updated>2011-11-15T11:36:32Z</updated>
    <link rel="self" title="CustomTable5" href="CustomTable5" />
    <entry m:etag="W/&quot;datetime'2011-11-05T08%3A29%3A45.8347866Z'&quot;">
        <id>https://eqmetest.table.core.windows.net/CustomTable5(PartitionKey='',RowKey='1')</id>
        <title type="text"></title>
        <updated>2011-11-15T11:36:32Z</updated>
        <author>
            <name />
        </author>
        <link rel="edit" title="CustomTable5" href="CustomTable5(PartitionKey='',RowKey='1')" />
        <category term="eqmetest.CustomTable5" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
        <content type="application/xml">
            <m:properties>
                <d:PartitionKey></d:PartitionKey>
                <d:RowKey>1</d:RowKey>
                <d:Timestamp m:type="Edm.DateTime">2011-11-05T08:29:45.8347866Z</d:Timestamp>
                <d:LastID m:type="Edm.Int32">54</d:LastID>
            </m:properties>
        </content>
    </entry>      
</feed>

我需要一个元素名称列表(在我的情况下应该是:PartitionKey,RowKey,Timestamp,LastID),如果有更多标签,我的列表应该来自 LastID.Value==一些身份

刚开始我已经写了这个:

 XmlNamespaceManager xs = new XmlNamespaceManager(new NameTable());
            xs.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
            xs.AddNamespace("a", "http://www.w3.org/2005/Atom");
            xs.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");

            var propertiesElement = document.XPathSelectElements("/a:feed/a:entry/a:content/m:properties", xs);

但我不知道如何结束)

【问题讨论】:

  • 您确定要在这里使用 XPath 吗?就我个人而言,我只会使用 LINQ to XML 方法,尤其是在涉及名称空间的情况下。
  • 如果您可以帮助使用 LINQ to XML 方法 - 没关系)

标签: c# xml linq-to-xml xml-parsing


【解决方案1】:
XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace atom = "http://www.w3.org/2005/Atom";

int id = 54;

XDocument doc = XDocument.Load("feed.xml");

List<XElement> properties = doc.Element(atom + "feed")
                        .Elements(atom + "entry")
                        .Elements(atom + "content")
                        .Elements(m + "properties")
                        .ToList();

XElement propertiesEl = properties.Count() > 1 ? properties.FirstOrDefault(p => (int)p.Element(m + "LastID") == id) : properties[0];

List<string> names = propertiesEl.Elements().Select(el => el.Name.LocalName).ToList();

【讨论】:

    【解决方案2】:

    试试这个,无论元素是否存在,它都应该工作,并且只抓取找到的第一个与搜索值匹配的元素:

        public static void Main()
        {
            var searchValue = "54";
            var xdoc = XDocument.Load(@"foo.xml");
    
            XNamespace nsD = "http://schemas.microsoft.com/ado/2007/08/dataservices";
            XNamespace nsM = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
            XNamespace nsDefault = "http://www.w3.org/2005/Atom";
    
            var foundElem = xdoc.Root
                .Elements(nsDefault + "entry")
                .Elements(nsDefault + "content")
                .Elements(nsM + "properties")
                .Elements(nsD + "LastID")
                .Where(e => e.Value == searchValue)
                .FirstOrDefault();
    
            if (foundElem != null)
            {
                var siblingElems = foundElem.Parent.Elements();
                foreach (var elem in siblingElems)
                {
                    Console.WriteLine("{0}", elem.Name.LocalName);
                }
            }
        }
    

    输出是:

    PartitionKey
    RowKey
    Timestamp
    LastID
    

    【讨论】:

    • 正如我所写它应该返回一个子元素名称列表 所以在我的情况下列表应该是这样的:PartitionKey,RowKey,Timestamp,LastID
    • 我更新了它以展示如何获得该输出。您可以根据需要调整代码...
    猜你喜欢
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2014-05-13
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多