如何解析xml
本文关键字:xml 何解析 | 更新日期: 2023-09-27 18:14:34
我有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/"datetime'2011-11-05T08%3A29%3A45.8347866Z'"">
<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。值= = someId
作为开头,我这样写:
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);
但我不知道如何结束
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();
试试这个,它应该在元素存在或不存在的情况下工作,并且只抓取第一个找到的与搜索值匹配的元素:
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