Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析XElement-get元素_C#_Xml_Linq_Linq To Xml - Fatal编程技术网

C# 解析XElement-get元素

C# 解析XElement-get元素,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我现在有一个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&

我现在有一个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>

网址
testcloudservice2
新云服务
西欧
dGVzdGNsb3Vkc2VydmljZTI=
创建
2013-04-02T09:40:27Z
2013-04-02T09:40:26Z
网址
测试123445
新云服务
西欧
dGVzdHRlc3R0ZXN0MTIzNDQ1
创建
2013-04-02T09:30:34Z
2013-04-02T09:30:34Z
我想获得每个
的服务名称和描述。我怎样才能得到这些呢?

试试:

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()
    });
尝试:

如果你想让他们结合起来:

var descAndNames = hostedServices.Elements().Select(
    x=> new { Name =  x.Descendants(y=>y.Name=="ServiceName").FirstOrDefault(),
              Description = x.Descendants(y=>y.Name=="Description")
                   .FirstOrDefault()
    });
印刷品:

Description        ServiceName 
testcloudservice2  New cloud service 
testtesttest123445 New cloud service 
印刷品:

Description        ServiceName 
testcloudservice2  New cloud service 
testtesttest123445 New cloud service 

您应该使用
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);

您应该使用
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);

这里棘手的一点是,您需要考虑名称空间

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

这里棘手的一点是,您需要考虑名称空间

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

aw,+1,真糟糕,如果
Element
返回null,我的解决方案将抛出
NullReferenceException
,而您将节点投射到
string
,这不会在
null
aw,+1,真糟糕,如果
Element
返回null,我的解决方案将抛出
NullReferenceException
,当您将节点强制转换为
string
,这不会在
null
上引发任何异常时,我相信所有服务都有名称:)我认为您的解决方案也是正确的,只要语法流畅,+1从meI返回,我相信所有服务都有名称:)我认为您的解决方案也是正确的,只要语法流畅,+1从我返回