C# 如何使用C使用XDocument导航到元素#
我有一个需要读取的XML文件,我无法移动到特定的元素,然后开始对子元素进行解析。 我需要开始阅读的元素是C# 如何使用C使用XDocument导航到元素#,c#,xml-parsing,linq-to-xml,C#,Xml Parsing,Linq To Xml,我有一个需要读取的XML文件,我无法移动到特定的元素,然后开始对子元素进行解析。 我需要开始阅读的元素是,导航到该元素然后开始阅读的最佳方法是什么 示例XML文件 <?xml version="1.0" encoding="UTF-8"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <ActivityId Correlation
,导航到该元素然后开始阅读的最佳方法是什么
示例XML文件
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="8eb2de20-8dff-4460-ffff-866ac948dfab" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">b921a5f3-8188-4021-9c6f-416bdf78f629</ActivityId>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetMenusResponse xmlns="http://www...">
<GetMenusResult>
<Header xmlns="http://www...">
<SessionId>{8CCCED2E-3D44-4200-BBAC-66660CA08692}</SessionId>
<CorrelationId>{AFFED12-AD36-4A2C-99FE-5E8D7CBC681D}</CorrelationId>
<TimeStamp>2018-09-14T11:36:21.5123749Z</TimeStamp>
</Header>
<ResponseStatus xmlns="http://www...">
<Status>OK</Status>
</ResponseStatus>
<ResponseBody xmlns="http://www...">
<Refreshed>2018-09-14T11:36:22.0115845Z</Refreshed>
<Menus>
<MenuCodes>
<MenuCode Code="AAA/A1" ShortText="Foo">
<ValidPrefix Prefix="V" />
<ValidPrefix Prefix="D" />
<ValidPrefix Prefix="N" />
</MenuCode>
<MenuCode Code="AAA/A2" ShortText="Foo2">
<ValidPrefix Prefix="D" />
<ValidPrefix Prefix="N" />
</MenuCode>
</MenuCodes>
</Menus>
</ResponseBody>
</GetMenusResult>
</GetMenusResponse>
</s:Body>
</s:Envelope>
b921a5f3-8188-4021-9c6f-416bdf78f629
{8CCCED2E-3D44-4200-BBAC-66660CA08692}
{AFFED12-AD36-4A2C-99FE-5E8D7CBC681D}
2018-09-14T11:36:21.5123749Z
好啊
2018-09-14T11:36:22.0115845Z
这里有一个非常详细的方法来获取该元素,大多数新手在这里忘记的重要一点是在名称空间中添加:
var doc = XDocument.Parse(xml);
var soapNs = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
var otherNs = XNamespace.Get("http://www...");
var menuCodes = doc
.Element(soapNs + "Envelope")
.Element(soapNs + "Body")
.Element(otherNs + "GetMenusResponse")
.Element(otherNs + "GetMenusResult")
.Element(otherNs + "ResponseBody")
.Element(otherNs + "Menus")
.Element(otherNs + "MenuCodes");
这里有一个非常详细的方法来访问该元素,大多数新手在这里忘记的重要一点是在名称空间中添加:
var doc = XDocument.Parse(xml);
var soapNs = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/");
var otherNs = XNamespace.Get("http://www...");
var menuCodes = doc
.Element(soapNs + "Envelope")
.Element(soapNs + "Body")
.Element(otherNs + "GetMenusResponse")
.Element(otherNs + "GetMenusResult")
.Element(otherNs + "ResponseBody")
.Element(otherNs + "Menus")
.Element(otherNs + "MenuCodes");
名称空间是一个问题。所以不要使用名称空间。请参阅下面的一行代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication68
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement menuCodes = doc.Descendants().Where(x => x.Name.LocalName == "MenuCodes").FirstOrDefault();
}
}
}
名称空间是一个问题。所以不要使用名称空间。请参阅下面的一行代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication68
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement menuCodes = doc.Descendants().Where(x => x.Name.LocalName == "MenuCodes").FirstOrDefault();
}
}
}