C# XML编写器将结果存储为字符串

C# XML编写器将结果存储为字符串,c#,xml,linq,c#-4.0,linq-to-xml,C#,Xml,Linq,C# 4.0,Linq To Xml,可能重复: DataTable dt=newdatatable(); Add(新的DataColumn(“Product_ID”,Type.GetType(“System.String”)); Add(新的DataColumn(“Product_Name”,Type.GetType(“System.String”)); Add(新的数据列(“产品价格”,Type.GetType(“System.Int32”)); 填充(“hello1”,dt,“product1”,1111); fillRow

可能重复:

DataTable dt=newdatatable();
Add(新的DataColumn(“Product_ID”,Type.GetType(“System.String”));
Add(新的DataColumn(“Product_Name”,Type.GetType(“System.String”));
Add(新的数据列(“产品价格”,Type.GetType(“System.Int32”));
填充(“hello1”,dt,“product1”,1111);
fillRows(“hello2”,dt,“product2”,2222);
填充(“hello3”,dt,“product3”,3333);
填充(“hello4”,dt,“product4”,4444);
var xmlColumnZero=dt.AsEnumerable().Select(col=>col[0].ToString()).ToArray();//第0行翻转倍增器
var xmlRowZero=dt.列;
字符串firstColumnHeader=dt.Columns[0].ToString();
//XmlDocument xmldoc=新的XmlDocument();
XmlWriter=XmlWriter.Create(“employees.xml”);
writer.WriteStartDocument();
编写人、书面声明(“员工”);
//xmlementfirst=xmldoc.CreateElement(“订单”);
//xmldoc.AppendChild(第一个);
foreach(数据列dc在dt.列中)
{
如果(dc.ToString()==firstColumnHeader)继续;
字符串firstcol=dc.ToString();
writer.writeStart元素(firstcol);
//xmlement colRoot=xmldoc.CreateElement(firstcol);
//第一,AppendChild(colRoot);
对于(int i=0;i)产品2
hello3>产品3
hello4>产品4
1111
hello2>2222
hello3>3333
hello4>4444

只需使用重载方法创建xml字符串。在这种情况下,所有输出都将写入
StringBuilder
,而不是文件:

StringBuilder builder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(builder);
//... build xml here

string xml = builder.ToString();
您还可以使用将xml写入
MemoryStream

更新

下面的扩展方法将生成您的xml字符串。默认情况下,它使用第一列作为元素名称,但您可以为包含元数据的列传递任何列索引。此外,我使用表名生成“Employees”标记,因此在创建数据表时提供名称
DataTable dt=new DataTable(“Employees”);

输出:

<Employees>
  <Product_Name>
    <hello1>product1</hello1>
    <hello2>product2</hello2>
    <hello3>product3</hello3>
    <hello4>product4</hello4>
  </Product_Name>
  <product_Price>
    <hello1>111</hello1>
    <hello2>222</hello2>
    <hello3>333</hello3>
    <hello4>444</hello4>
  </product_Price>
</Employees>

产品1
产品2
产品3
产品4
111
222
333
444
使用and而不是文件:

StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
writer.WriteStartDocument();
//...
writer.WriteEndDocument();
var myXmlString = sb.ToString(); //myXmlString will contain the XML

而不是创建一个文件

XmlWriter writer = XmlWriter.Create("employees.xml");
您可以使用字符串流

StringWriter sw = new StringWriter();
XmlWriter writer = XmlWriter.Create(sw);
...
...
// sw.ToString(); // string output

fillRows
做什么?你是不是故意在你的问题中加入格式错误的XML?我认为这会让问题变得混乱。如果你想保存字符串,为什么要使用XmlWriter?请给出一个你想要的示例。最佳方法。非常感谢。@user1650470欢迎:)请记住,生成的xml没有xml声明,正如您在预期输出中所希望的那样。可以创建没有表名的xml,即此处的节点。我尝试了几种方法,但对我无效。我应该如何修改扩展方法。感谢advance@user1650470实际上,您需要根节点。如果需要,它将不是格式良好的xml我现在不需要,因为我有一个函数,它接受两个参数:根节点(表名)和xml,并从中创建一个新的xml。
string xml = dt.ToXml();
<Employees>
  <Product_Name>
    <hello1>product1</hello1>
    <hello2>product2</hello2>
    <hello3>product3</hello3>
    <hello4>product4</hello4>
  </Product_Name>
  <product_Price>
    <hello1>111</hello1>
    <hello2>222</hello2>
    <hello3>333</hello3>
    <hello4>444</hello4>
  </product_Price>
</Employees>
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
writer.WriteStartDocument();
//...
writer.WriteEndDocument();
var myXmlString = sb.ToString(); //myXmlString will contain the XML
XmlWriter writer = XmlWriter.Create("employees.xml");
StringWriter sw = new StringWriter();
XmlWriter writer = XmlWriter.Create(sw);
...
...
// sw.ToString(); // string output