0

現在、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>

それぞれの ServiceName と Description を取得したいと思います<HostedService>。どうすればこれらを入手できますか?

4

4 に答える 4

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")
            };

ServiceNameこれにより、プロパティおよびを持つ無名オブジェクトのシーケンスが返されますDescription。使用法:

foreach(var service in hostedServices)
   Debug.WriteLine(service.ServiceName + ": " + service.Description);
于 2013-04-02T09:50:25.783 に答える
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 
于 2013-04-02T09:50:01.823 に答える
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()
    });
于 2013-04-02T09:47:07.160 に答える
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);
于 2013-04-02T10:01:16.397 に答える