替代的方式将数据表转换为自定义XML
问题描述:
有没有一种方法我可以创建自DataTable的XML,而无需使用替代的方式将数据表转换为自定义XML
-
一个
StringWriter
:System.IO.StringWriter writer = new System.IO.StringWriter(); yourDataTable.WriteXml(writer, XmlWriteMode.WriteSchema, true);
不适合我的工作它将后代作为列标题。我想要第一行作为后代。
对DataColumns和DataRows进行迭代。
我在阅读关于LINQ-to-DataTable和LINQ-to-XML。
LINQ查询能帮助我吗?
答
以下是使用XmlDocument
类来生成XML
XmlDocument xDoc = new XmlDocument();
XmlElement rootElement = xDoc.CreateElement("Root");
XmlElement customElement = xDoc.CreateElement("customElement");
XmlAttribute xAtt = xDoc.CreateAttribute("customAttribute");
xAtt.Value = "attval";
customElement.Attributes.Append(xAtt);
rootElement.AppendChild(customElement);
xDoc.AppendChild(rootElement);
的LINQ到XML库XDocument
遵循类似的模型的一个例子。你应该看看文档并利用它。
这是一个XDocument类的链接,它是Linq to XML类http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx –