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



可能的重复:
将数据表转换为自定义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编写器方法将所有内容存储到程序位置的 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>

只需使用重载方法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 字符串。默认情况下,它使用第一列作为元素名称,但您可以为包含元数据的列传递任何列索引。此外,我使用表名来生成"员工"标签,因此在创建数据表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>

使用StringBuilder并使用StringBuilder而不是文件创建XmlWriter

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

最新更新