我有两个实体
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 没有正确填充。(抛出对象引用错误)。
如何纠正?