XML Writer将结果存储到字符串

时间:2012-11-09 17:49:05

标签: c# xml linq c#-4.0 linq-to-xml

  

可能重复:
  Alternative ways to convert data table to customized XML

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Product_ID", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Product_Name", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("product_Price", Type.GetType("System.Int32")));
fillRows("hello1", dt, "product1", 1111);
fillRows("hello2", dt, "product2", 2222);
fillRows("hello3", dt, "product3", 3333);
fillRows("hello4", dt, "product4", 4444);


var xmlColumnZero = dt.AsEnumerable().Select(col => col[0].ToString()).ToArray() ; // row 0 turnovermultiplieer
var xmlRowZero = dt.Columns;
string firstColumnHeader = dt.Columns[0].ToString();
// XmlDocument xmldoc = new XmlDocument();
XmlWriter writer = XmlWriter.Create("employees.xml");
writer.WriteStartDocument();
writer.WriteStartElement("Employees");

// XmlElement first = xmldoc.CreateElement("Order");
//xmldoc.AppendChild(first);
foreach (DataColumn dc  in dt.Columns ) 
{

    if (dc.ToString() == firstColumnHeader) continue;
    string firstcol = dc.ToString();
    writer.WriteStartElement(firstcol);
    // XmlElement colRoot = xmldoc.CreateElement(firstcol);
    //first.AppendChild(colRoot);
    for (int i = 0 ; i <dt.Rows.Count && i< xmlColumnZero.Length ; i++)
    {
        string firstrow = xmlColumnZero[i];
        string dtagaga = dt.Rows[i][dc].ToString();
        writer.WriteElementString(firstrow, dtagaga);
       // XmlElement rowRoot = xmldoc.CreateElement(firstrow, dtagaga);
        //colRoot.AppendChild(rowRoot);


    }
    writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();

我希望在创建XMLWriter时将XMl存储到字符串中。

我是否可以通过另一种方式创建表格中的xml

XML应该看起来像

xml writer方法将所有内容存储到程序位置的xml文件中。更喜欢保存字符串

<Employees>
<Product_Name>
<hello1>product1</hello1>
hello2>product2</hello2>
hello3>product3</hello3>
hello4>product4</hello4>
</product_name>
<product_Price>
<hello1>1111</hello1>
hello2>2222</hello2>
hello3>3333</hello3>
hello4>4444</hello4>
</product_Price>
</Employees>

3 个答案:

答案 0 :(得分:1)

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

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

string xml = builder.ToString();

您也可以使用XmlWriter.Create(Stream output)将xml写入MemoryStream

Stream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream);
// ... build xml here

stream.Position = 0;
string xml = new StreamReader(stream).ReadToEnd();

<强>更新

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

public static string ToXml(this DataTable table, int metaIndex = 0)
{
    XDocument xdoc = new XDocument(
        new XElement(table.TableName,
            from column in table.Columns.Cast<DataColumn>()
            where column != table.Columns[metaIndex]
            select new XElement(column.ColumnName,
                from row in table.AsEnumerable()
                select new XElement(row.Field<string>(metaIndex), row[column])
                )
            )
        );

    return xdoc.ToString();
}

用法非常简单:

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>

答案 1 :(得分:1)

使用StringBuildercreate the XmlWriter using the StringBuilder代替文件:

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

答案 2 :(得分:1)

而不是创建文件

XmlWriter writer = XmlWriter.Create("employees.xml");

您可以使用字符串流

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