将数据表转换为自定义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 to xml库XDocument
遵循类似的模型。 你应该查看文档并使用它。
上述就是C#学习教程:将数据表转换为自定义XML的替代方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1031259.html