简体   繁体   中英

xml descendants looping issue in c#

I am unable to get values from xml while looping, i want to get values of each and every element inside HostedService. For eg service name, Url etc.

Below is my code

XDocument doc;
using (Stream input = System.IO.File.OpenRead(@"D:\Test.xml"))
{
    doc = XDocument.Load(input);

    foreach (var events in doc.Root.Descendants("HostedServices")) // loop through all events
    {

    }
}

XML example:

<HostedServices     xmlns="http://schemas.microsoft.com/windowsazure"xmlns:i="http://www.w3.org/2001/XMLSchema-instance">                 
<HostedService>
    <Url>https://www.google.com</Url>
    <ServiceName>sharepoint2013vm</ServiceName>
    <HostedServiceProperties>
      <Description i:nil="true" />
      <Location>East Asia</Location>
      <Label>c2hhcmVwb2ludDIwMTN2bQ==</Label>
      <Status>Created</Status>
      <DateCreated>2015-01-13T03:42:21Z</DateCreated>
      <DateLastModified>2015-01-13T03:42:46Z</DateLastModified>
      <ExtendedProperties>
        <ExtendedProperty>
          <Name>ResourceGroup</Name>
          <Value>sharepoint2013vm</Value>
        </ExtendedProperty>
        <ExtendedProperty>
          <Name>ResourceLocation</Name>
          <Value>East Asia</Value>
        </ExtendedProperty>
      </ExtendedProperties>
    </HostedServiceProperties>
  </HostedService>
  <HostedService>

  </HostedService>
</HostedServices>

Here is one way to do it.

        XDocument doc;

        using (Stream input = System.IO.File.OpenRead("XMLFile1.xml"))
        {
            doc = XDocument.Load(input);

            XmlNamespaceManager nm = new XmlNamespaceManager(new NameTable());
            XNamespace ns = doc.Root.GetDefaultNamespace();
            nm.AddNamespace("ns", ns.NamespaceName);

            foreach (var hostedService in doc.Root.XPathSelectElements("ns:HostedService",nm)) // loop through all events
            {
                if (hostedService.XPathSelectElement("ns:ServiceName", nm) != null)
                {
                    var service = hostedService.XPathSelectElement("ns:ServiceName",nm).Value;
                }
                if (hostedService.XPathSelectElement("ns:Url",nm) != null)
                {
                    var url = hostedService.XPathSelectElement("ns:Url",nm).Value;
                }
            }
        }

Use this:

XDocument doc = XDocument.Load(@"D:\Test.xml");

to load the XML file in memory.

You have to use XNamespace since your original XML declares one:

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

To loop through all elements of <HostedService> use this:

foreach (var events in doc.Descendants(ns+"HostedService").Elements()) 
{

}

The above foreach will give you access to all child Elements() of <HostedService> , ie

  1. <Url>
  2. <ServiceName> and
  3. <HostedServiceProperties>

If, on the other side, you want to access all elements (including child elements etc.) below <HostedService> then use the following ;

 foreach (var events in doc.Descendants(ns + "HostedService").DescendantNodes().OfType<XElement>())
 {
 }

The above gives you access to:

  1. <Url>
  2. <ServiceName>
  3. <HostedServiceProperties>
  4. <Description>
  5. <Location>
  6. <Label>
  7. ... etc.

You need to specify XML namespace with your element name:

XNamespace ns = "http://schemas.microsoft.com/windowsazure";
foreach (var events in doc.Root.Descendants(ns + "HostedServices"))
{

}

Have a look at Working with XML Namespaces for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM