如果您使用.net 3.5,您可以使用XLINQ。這是http://blogs.msdn.com/wriju/archive/2007/02/20/xlinq-create-xml-from-object-using-linq.aspx
的例子如果一個谷歌上"xlinq create"你會發現許多例子(你很可能是最感興趣的RSS例子,我想。
var objCust = new[]
{
new {CustID = 2, CustName = "Sumitra", Phone = "123-123-1236"},
new {CustID = 3, CustName = "Wriju", Phone = "123-123-1235"},
new {CustID = 4, CustName = "Writam", Phone = "123-123-1234"},
new {CustID = 1, CustName = "Debajyoti", Phone = "123-123-1237"}
};
XElement _customers = new XElement("customers",
from c in objCust
orderby c.CustID //descending
select new XElement("customer",
new XElement("name", c.CustName),
new XAttribute("ID", c.CustID),
new XElement("phone", c.Phone)
)
);
Console.WriteLine(_customers);
輸出看起來像這樣:
<customers>
<customer ID="1">
<name>Debajyoti</name>
<phone>123-123-1237</phone>
</customer>
<customer ID="2">
<name>Sumitra</name>
<phone>123-123-1236</phone>
</customer>
<customer ID="3">
<name>Wriju</name>
<phone>123-123-1235</phone>
</customer>
<customer ID="4">
<name>Writam</name>
<phone>123-123-1234</phone>
</customer>
</customers>
我不知道該輸入流是XML聽起來更像是它的其他數據被轉換爲XML – 2009-02-10 04:53:10
@Joel,是的,它是從一個TCP流,它被解析,並根據建立的XML。那數據。 – Jeremy 2009-02-11 23:29:35