我有一个由c#代码创建的下面的xml数据,这不是我所需的格式。任何人都可以建议如何完成这项工作吗?
C#代码:
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = Encoding.UTF8;
using (XmlWriter writer = XmlWriter.Create("SNP.xml",xmlWriterSettings))
{
writer.WriteStartDocument();
writer.WriteStartElement("Request","http://www.ABC.com/submit");
foreach (DataRow row in ExcelData.Rows)
{
writer.WriteStartElement(ExcelData.Rows[counter]["MyDATA"].ToString());
writer.WriteElementString("DATA1", ExcelData.Rows[counter]["DATA1"].ToString());
writer.WriteElementString("DATA2", ExcelData.Rows[counter]["DATA2"].ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
c#生成的数据:
<?xml version="1.0" encoding="utf-8" ?>
- <Request xmlns="http://www.timvw.be/ns">
- <MyData>
<DATA1>91.689117127891</DATA1>
<DATA2>0.18242169290979</DATA2>
</MyData>
</Request>
但是我需要以下格式的xml数据:
<?xml version="1.0" encoding="utf-16" ?>
- <Request xmlns="http://www.ABC.com/submit" xmlns:l="http://www.ABC.com/link" l:source="SRC" l:date="2014-03-05">
- <MyData l:identifier="PRAV" l:value="151.19448366182007269092408546">
<DATA1 l:value="0.151328110447635" />
<DATA2 l:value="0.6461191930062688087600920641" />
<DATA3 l:value="0.144793773777417" />
</MyData>
</Request>
任何人都可以在c#代码中建议需要设置的内容
答案 0 :(得分:1)
如果您需要将值写为XmlAttribute,请尝试以下方法:
writer.WriteStartElement("DATA1");
writer.WriteAttributeString("l:value", ExcelData.Rows[counter]["DATA1"].ToString());
writer.WriteEndElement();