2

我有这个 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>
4

2 回答 2

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));
于 2012-12-01T17:56:11.487 回答
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;
于 2012-12-01T18:12:26.263 回答