大家好我有3類添加數據後如下C#類爲xml使用LINQ
public class Main
{
public List<B> BList{ get; set; }
}
public class B
{
public B()
{
ListA = new List<A>();
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<A> ListA { get; set; }
}
public class A
{
public int Rating { get; set; }
public int Weightage { get; set; }
}
我試圖使用LINQ如下將其轉換爲XML
Main main = new Main();
var xEle = new XElement("Root",
from x in main.BList
from te in x.A
select new XElement("Child",
new XElement("Weightage", te.Weightage),
new XElement("FName", x.FirstName),
new XElement("LName", x.Email)));
這是給我輸出如下
<Root>
<Child>
<Weightage>20</Weightage>
<FName>ABC</FName>
<LName>[email protected]</LName>
</Child>
<Child>
<Weightage>20</Weightage>
<FName>ABC</FName>
<LName>[email protected]</LName>
</Child>
</Root>
我需要的是如下
<Root>
<Child>
<Weightage>10</Weightage>
<FName>ABC</FName>
<LName>[email protected]</LName>
</Child>
<Child>
<Weightage>20</Weightage>
<FName>ABC</FName>
<LName>[email protected]</LName>
</Child>
</Root>
提琴手這裏https://dotnetfiddle.net/x6Hj01
你能某處添加一些字符來表示(電流輸出與預期輸出之間)的差異,我不能告訴ATM - 使用在線比較,現在看到的差異(20的權重應該是10) – EpicKip
也許使用序列化?轉換爲xml字符串並返回到對象時,我發現它很有用。 – japesu
輸出的孩子的體重是相同的,即20 –