user1869033
user1869033

Reputation: 21

query linq to xml

I've this xml file. I'm trying to obtain all dbistance for a specific servername attribute. For each dbistance information group i want add at listbox the result of concatenation like this DEFAULT - DBCATALOG - USERNAME - PASSWORD - 1433 please me...beginning to hate linq :)

    <?xml version="1.0" encoding="utf-8" ?>
<root>
  <server servername="SRV01">
    <dbistance>
      <istancename>DEFAULT</istancename>
      <catalog>DBCATALOG01</catalog>
      <username>USERNAME</username>
      <password>PASSWORD</password>
      <port>1433</port>
    </dbistance>
    <dbistance>
      <istancename>DEFAULT</istancename>
      <catalog>DBCATALOG02</catalog>
      <username>USERNAME</username>
      <password>PASSWORD</password>
      <port>1433</port>
    </dbistance>
  </server>

  <server servername="SRV02">
    <dbistance>
      <istancename>DEFAULT</istancename>
      <catalog>DBCATALOG01</catalog>
      <username>USERNAME</username>
      <password>PASSWORD</password>
      <port>1433</port>   
    </dbistance>
  </server>
</root>

Upvotes: 2

Views: 64

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236318

I'd split parsing xml and results formatting into two actions. But here is a single query:

XDocument xdoc = XDocument.Load(path_to_xml);
string serverName = "SRV01";
IEnumerable<string> dbInstances =
            xdoc.Descendants("server")
                .Where(s => (string)s.Attribute("servrname") == serverName)
                .Descendants("dbinstance")
                .Select(db => String.Format("{0} - {1} - {2} - {3} - {4}", 
                    (string)db.Element("istancename"),
                    (string)db.Element("catalog"),
                    (string)db.Element("username"),
                    (string)db.Element("password"),
                       (int)db.Element("port")));

BTW you have typo in istancename tag.


Consider also creating class which will hold data and do formatting for you:

public class DbInstance
{
    public string InstanceName { get; set; }
    public string Catalog { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public int Port { get; set; }

    public override ToString()
    {
        return String.Format("{0} - {1} - {2} - {3} - {4}", 
                  InstanceName, Catalog, UserName, Password, Port));
    }
}

Parse xml (you can do it in separate method, which will return collection of DbInstance objects):

XDocument xdoc = XDocument.Load(path_to_xml);
string serverName = "SRV01";
IEnumerable<DbInstance> dbInstances =
            xdoc.Descendants("server")
                .Where(s => (string)s.Attribute("servrname") == serverName)
                .Descendants("dbinstance")
                .Select(db => new DbInstance() {
                    InstanceName = (string)db.Element("istancename"),
                    Catalog = (string)db.Element("catalog"),
                    UserName = (string)db.Element("username"),
                    Password = (string)db.Element("password"),
                    Port = (int)db.Element("port") });

And just assign instances to listbox (overridden ToString method will do the job):

listBox.DataSource = dbInstances;

Upvotes: 1

roman
roman

Reputation: 117540

try this

var doc = XDocument.Load(new StringReader(xml));
var result = doc.Root
                .Descendants("dbistance")
                .Select(elem => elem.Descendants()
                                    .Select(elem2 => elem2.Value)
                                    .Aggregate((current, next) => current + " - " + next));

Upvotes: 1

Related Questions