无法使用XDocument从XML正确加载子记录

时间:2013-05-02 07:20:24

标签: c# linq-to-xml

我有两个实体

public class DbServer
    {
        public int ServerId { set; get; }
        public string Name { set; get; }
        public string Address { set; get; }
        public List<DbDetail> DbDetails = new List<DbDetail>();
    }
    public class DbDetail
    {
        public int DbId { set; get; }
        public string DbName { set; get; }
        public string UserName { set; get; }
        public string PassWord { set; get; }
        public List<string> ConfiguredRules = new List<string>();

    }

,XML文件在

下面
<DataBaseServers>
      <DataBaseServer id ="1" title="IISDevdb1" address="iisdevdb1.mcafee.int\iisdevdb1,1515">
        <DataBases>
          <database id="1" name="ebiz" userID="se_user" password="se_user_pw!"> </database>
          <database id="2" name="oneclick" userID="se_user" password="se_user_pw!"> </database>
          <database id="3" name="EbizStats" userID="se_user" password="se_user_pw!"> </database>
          <database id="4" name="ebizlite" userID="se_user" password="se_user_pw!"> </database>
          <database id="5" name="EbizErrors" userID="se_user" password="se_user_pw!"> </database>
          <database id="6" name="machinetracker" userID="se_user" password="se_user_pw!"> </database>
          <database id="7" name="mast" userID="se_user" password="se_user_pw!"> </database>
          <database id="8" name="subscribe" userID="se_user" password="se_user_pw!"> </database>
          <database id="9" name="OFFER" userID="se_user" password="se_user_pw!"> </database>
          <database id="10" name="Session" userID="se_user" password="se_user_pw!"> </database>
          <database id="11" name="Reconciliation" userID="se_user" password="se_user_pw!"> </database>
          <database id="12" name="smartmessaging" userID="se_user" password="se_user_pw!"> </database>
          <database id="13" name="Payment" userID="se_user" password="se_user_pw!"> </database>
          <database id="46" name="SQLEye" userID="se_user" password="se_user_pw!"> </database>
          <database id="50" name="VCAPI" userID="se_user" password="se_user_pw!"> </database>         
        </DataBases>
      </DataBaseServer>
      <DataBaseServer id ="3" title="172.16.216.232" address="172.16.216.232">
        <DataBases>
          <database id="34" name="ebiz" userID="ebizsol" password="ebizsol_pw"> </database>
          <database id="35" name="oneclick" userID="ebizsol1click" password="ebizsol1click_pw"> </database>
          <database id="36" name="EbizStats" userID="ebizsol" password="ebizsol_pw"> </database>
          <database id="37" name="ebizlite" userID="ebizlite" password="ebizlite_pw"> </database>
          <database id="38" name="EbizErrors" userID="ebizsol" password="ebizsol_pw"> </database>
          <database id="39" name="machinetracker" userID="mtrackuser" password="mtrackUser_pw"> </database>
          <database id="40" name="mast" userID="mast_user" password="mast_user_pw"> </database>
          <database id="41" name="subscribe" userID="subscribeuser" password="subscribeuser"> </database>
          <database id="42" name="OFFER" userID="offer" password="offer_pw"> </database>
          <database id="43" name="Session" userID="Session" password="session_pw"> </database>
          <database id="44" name="smartmessaging" userID="ebizsol" password="ebizsol_pw"> </database>
          <database id="45" name="Payment" userID="ebizsol" password="ebizsol_pw"> </database>
          <database id="47" name="monitoring" userID="ebizsol" password="ebizsol_pw"> </database>          
        </DataBases>
      </DataBaseServer>      
    </DataBaseServers>

我正在尝试将这两个实体填充到

下面
XDocument xdoc = XDocument.Load(dbServerPath);
var res = from dbServer in xdoc.Descendants("DataBaseServer")
                     select new DbServer
                     {
                         ServerId = Convert.ToInt32(dbServer.Attribute("id").Value),
                         Name = dbServer.Attribute("title").Value,
                         Address = dbServer.Attribute("address").Value,
                          DbDetails = (from dbDetail in xdoc.Descendants("DataBases")
                                       select new DbDetail
                                       {
                                           DbId = Convert.ToInt32(dbDetail.Attribute("id").Value),
                                           DbName = dbDetail.Attribute("name").Value
                                       }).ToList()

                     };

但DBDetails没有正确填充。(对象引用错误正在抛出)。

如何纠正?

1 个答案:

答案 0 :(得分:2)

根据您的固定拼写错误,以下内容将为您提供所需的信息。

XElement xdoc = XDocument.Load("dbServerPath").Element("DataBaseServers");
var res = from dbServer in xdoc.Elements("DataBaseServer")
    select new DbServer
    {
        ServerId = Convert.ToInt32(dbServer.Attribute("id").Value),
        Name = dbServer.Attribute("title").Value,
        Address = dbServer.Attribute("address").Value,
        DbDetails = (from dbDetail in dbServer.Element("DataBases").Elements("database")
                     select new DbDetail
                     {
                         DbId = Convert.ToInt32(dbDetail.Attribute("id").Value),
                         DbName = dbDetail.Attribute("name").Value 
                     }).ToList()
    };

您的代码不起作用的原因是因为当您加载文档然后启动linq查询时,您正在检查DataBaseServer元素,但是在您正在搜索的级别它们不存在,因此您需要先获取DataBaseServers元素然后从该点加载服务器。相同的逻辑适用于数据库详细信息填充 - 您需要获取包含所有数据库详细信息记录的第一个元素。