2013-04-02 113 views
0

我目前有一個XElement。我使用它來打印:解析XElement - 獲取元素

System.Diagnostics.Debug.WriteLine(hostedServices); 

結果:

<HostedServices xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <HostedService> 
    <Url>url</Url> 
    <ServiceName>testcloudservice2</ServiceName> 
    <HostedServiceProperties> 
     <Description>New cloud service</Description> 
     <Location>West Europe</Location> 
     <Label>dGVzdGNsb3Vkc2VydmljZTI=</Label> 
     <Status>Created</Status> 
     <DateCreated>2013-04-02T09:40:27Z</DateCreated> 
     <DateLastModified>2013-04-02T09:40:26Z</DateLastModified> 
     <ExtendedProperties /> 
    </HostedServiceProperties> 
    </HostedService> 
    <HostedService> 
    <Url>url</Url> 
    <ServiceName>testtesttest123445</ServiceName> 
    <HostedServiceProperties> 
     <Description>New cloud service</Description> 
     <Location>West Europe</Location> 
     <Label>dGVzdHRlc3R0ZXN0MTIzNDQ1</Label> 
     <Status>Created</Status> 
     <DateCreated>2013-04-02T09:30:34Z</DateCreated> 
     <DateLastModified>2013-04-02T09:30:34Z</DateLastModified> 
     <ExtendedProperties /> 
    </HostedServiceProperties> 
    </HostedService> 
</HostedServices> 

我想獲得每<HostedService>的服務名稱和說明。我怎樣才能得到這些?

回答

1
var xml = XDocument.Parse(s); 

XNamespace ns = "http://schemas.microsoft.com/windowsazure"; 

var descriptions = xml.Descendants(ns + "HostedService") 
         .Select(prop => 
           new { Description = prop.Element(ns + "ServiceName").Value, 
            ServiceName = prop.Descendants(ns + "Description").First().Value}); 

//LINQPad specific print call     
descriptions.Dump(); 

打印:

Description  ServiceName 
testcloudservice2 New cloud service 
testtesttest123445 New cloud service 
+0

我相信所有的服務都有名字:)我認爲你的解決方案也是正確的,只要流利的語法,我的+1返回 –

0

嘗試:

var descriptions = hostedServices.Elements().Select(
    x=> x.Descendants(y=>y.Name=="Description").FirstOrDefault()); 
var serviceNames = hostedServices.Elements().Select(
    x=> x.Descendants(y=>y.Name=="ServiceName").FirstOrDefault()); 

如果你想他們配上:

var descAndNames = hostedServices.Elements().Select(
    x=> new { Name = x.Descendants(y=>y.Name=="ServiceName").FirstOrDefault(), 
       Description = x.Descendants(y=>y.Name=="Description") 
        .FirstOrDefault() 
    }); 
+0

謝謝,但。選擇是不可能上的XElement .. – Bv202

+0

真 - >我」米固定它。 –

2

您應該使用XNamespace選擇元素:

XDocument xdoc = XDocument.Load(path_to_xml); 
XNamespace ns = "http://schemas.microsoft.com/windowsazure"; 
var hostedServices = 
      from s in xdoc.Descendants(ns + "HostedService") 
      select new 
      { 
       ServiceName = (string)s.Element(ns + "ServiceName"), 
       Description = (string)s.Element(ns + "HostedServiceProperties") 
             .Element(ns + "Description") 
      }; 

這將返回具有屬性ServiceNameDescription的匿名對象序列。用法:

foreach(var service in hostedServices) 
    Debug.WriteLine(service.ServiceName + ": " + service.Description); 
+0

aw,+1,如果'Element'返回,我的解決方案會拋出'NullReferenceException' null,而您將節點轉換爲'string',它不會在'null'上拋出任何異常 –

0

這裏棘手的時刻,你需要採取命名空間考慮:

XNamespace ns = "http://schemas.microsoft.com/windowsazure"; 
var serviceNames = element.Descendants(ns + "ServiceName"); 
var descriptions = element.Descendants(ns + "Description"); 

var serviceNameValues = serviceNames.Select(x => x.Value); 
var descriptionValues = descriptions.Select(x => x.Value);