查询linq到xml

时间:2012-12-01 17:33:22

标签: xml linq linq-to-xml

我有这个xml文件。我正在尝试获取特定servername属性的所有dbistance。 对于每个dbistance信息组,我想在列表框中添加连接的结果 像这样DEFAULT - DBCATALOG - USERNAME - PASSWORD - 1433 请我...开始讨厌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>

2 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:1)

我将解析xml并将格式化为两个动作。但这是一个查询:

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

顺便说一句,你在istancename标签中输了错字。


还要考虑创建一个包含数据并为您进行格式化的类:

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

解析xml(你可以在单独的方法中执行它,它将返回DbInstance对象的集合):

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

只需将实例分配给列表框(覆盖ToString方法即可完成工作):

listBox.DataSource = dbInstances;