将数据表转换为自定义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查询可以帮助我吗?

这是使用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 to xml库XDocument遵循类似的模型。 你应该查看文档并使用它。