替代的方式将数据表转换为自定义XML

问题描述:

有没有一种方法我可以创建自DataTable的XML,而无需使用替代的方式将数据表转换为自定义XML

  1. 一个StringWriter

    System.IO.StringWriter writer = new System.IO.StringWriter(); 
    yourDataTable.WriteXml(writer, XmlWriteMode.WriteSchema, true); 
    

    不适合我的工作它将后代作为列标题。我想要第一行作为后代。

  2. 对DataColumns和DataRows进行迭代。

我在阅读关于LINQ-to-DataTable和LINQ-to-XML。

LINQ查询能帮助我吗?

+0

这是一个XDocument类的链接,它是Linq to XML类http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx –

以下是使用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遵循类似的模型的一个例子。你应该看看文档并利用它。