如果您使用.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