简体   繁体   中英

How do I get a string array of these xml values?

I was attempting to use linq to xml, but I'm pretty new to it.

This is the xml:

<BrandHosts>
  <Brand>
    <ResourceName>BrandInfo_AAA</ResourceName>
    <Hosts>
      <Host>www.aaa.com</Host>
      <Host>portal.aaa.com</Host>
      <Host>aaa.com</Host>
    </Hosts>
  </Brand>
  <Brand>
    <ResourceName>BrandInfo_BBB</ResourceName>
    <Hosts>
      <Host>www.bbb.com</Host>
      <Host>bbb.com</Host>
      <Host>portal.bbb.com</Host>
    </Hosts>
  </Brand>
  <Brand>
    <ResourceName>BrandInfo_CCC</ResourceName>
    <Hosts>
      <Host>www.CCC.com</Host>
    </Hosts>
  </Brand>
  <Brand>
    <ResourceName>BrandInfo_DDD</ResourceName>
    <Hosts>
      <Host>www.DDD.com</Host>
    </Hosts>
  </Brand>
</BrandHosts>

I will have a string value that has the resource name of what I need to pull out of this xml. So my parameter, for example, might be "BrandInfo_BBB". And I would need to return a string array containing all of the hosts from that block. Can I do this with linq to xml?

First of all: load your XML into XDocument object and prepare result variable:

var doc = XDocument.Load("Input.txt");
string[] hosts;

Then you can query the document. I assumed that ResourceName is unique across input XML .

var resourceName = "BrandInfo_DDD";

var brand = doc.Root.Elements("Brand").SingleOrDefault(b => (string)b.Element("ResourceName") == resourceName);

if (brand != null)
{
    hosts = brand.Element("Hosts")
                 .Elements("Host")
                 .Select(h => (string)h)
                 .ToArray();
}

For non-unique ResourceName

var brands = doc.Root.Elements("Brand").Where(b => (string)b.Element("ResourceName") == resourceName).ToArray();

string[] hosts;

if (brands.Length > 0)
{
    hosts = brands.SelectMany(b => b.Element("Hosts")
                                    .Elements("Host")
                                    .Select(h => (string)h)
                                    ).ToArray();
}
string toSearch = "BrandInfo_BBB";

XDocument xDoc = XDocument.Load("XMLFile1.xml");


string[] strArr = xDoc.Descendants("Brand").Where(n => n.Element("ResourceName").Value == toSearch).Single()
                      .Descendants("Host").Select(h => h.Value).ToArray();

Just offering another alternative...

XDocument doc = XDocument.Load("BrandHosts.xml");
string resourceName = "BrandInfo_DDD";

var resources = doc
    .Descendants("Brand")
    .Where(n => n.Element("ResourceName").Value == resourceName);

var hosts = resources.Any() ?
    resources.Descendants("Host").Select(h => h.Value) :
    Enumerable.Empty<string>();

By using Enumerable.Empty<> above (when no resources are returned) you ensure that hosts will never be null and therefore it is always safe to use it immediately...

string[] vals = hosts.ToArray();

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