我想在C#中使用XmlWriter类生成以下格式的XML:
<?xml version="1.0" ?>
<root>
<data>
<entry Attrib1="" Attrib2="91.3467" Attrib3="95.3052" Attrib4="6.4722" />
<entry Attrib1="" Attrib2="91.3467" Attrib3="95.3052" Attrib4="6.4722" />
</data>
</root>
我对XmlWriter类和C#一般都是新手,我尝试编写用于生成具有上述格式的文件的代码,但该尝试不成功
var xmlWriter = XmlWriter.Create(filename);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("data");
xmlWriter.WriteStartElement("entry");
xmlWriter.WriteAttributeString("attrib1", "value1");
xmlWriter.WriteAttributeString("attrib2", "value2");
xmlWriter.Close();
另外,属性的名称可以包含非法的XML字符,这就是我阅读XMLWriter
的原因,因为它似乎从属性的名称中删除了那些非法字符,例如“this is attribute”这样的名称在写入生成的XML时,应将“1”简化为类似“this_is_attribute_1”的内容,如何使用XML
生成此类XmlWriter
。简而言之,生成的XML行就像这样
<entry P_B_Pe="" P_E_Pe="91.3467" Custom_Price="95.3052" C_Yield="6.4722" Average_Life="" />
答案 0 :(得分:1)
你几乎得到了它......
var xmlWriter = XmlWriter.Create(filename);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("root");
xmlWriter.WriteStartElement("data");
xmlWriter.WriteStartElement("entry");
xmlWriter.WriteAttributeString("attrib1", "value1");
xmlWriter.WriteAttributeString("attrib2", "value2");
xmlWriter.WriteEndElement(); // entry
xmlWriter.WriteStartElement("entry");
xmlWriter.WriteAttributeString("attrib1", "value1");
xmlWriter.WriteAttributeString("attrib2", "value2");
xmlWriter.WriteEndElement(); // entry
xmlWriter.WriteEndElement(); // data
xmlWriter.WriteEndElement(); // root
xmlWriter.WriteEndDocument();
xmlWriter.Close();
默认情况下,XmlWriter
将对原始数据或属性值中通常无效的字符进行编码,以便在使用读取器解码XML时它们会返回,但属性和元素名称仍然必须有效。如果你想以某种特殊的方式处理那些与之不同的无效字符,你需要根据你想要建立的规则自己做这些,例如:
xmlWriter.WriteAttributeString(MyXmlExtensions.EncodeXmlAttributeName("this is normally an invalid attribute name"), "value1");
class MyXmlExtensions
{
public string EncodeXmlAttributeName(string decoded)
{
// not that you'll likely need to enhance this with whatever rules you want but haven't specified
return decoded.Replace(" ", "_");
}
public string DecodeXmlAttributeName(string encoded)
{
// not that you'll likely need to enhance this with whatever rules you want but haven't specified
return encoded.Replace("_", " ");
}
}
如果您希望输出看起来很漂亮(标签,多行等),您还需要在XmlWriterSettings
的调用中使用XmlWriter.Create
。
答案 1 :(得分:0)
尝试XML Linq
<button type="button" class="btn btn-primary" ng-click="ctrl.changeDisplay()">Save Add Another</button>
然后没有任何xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
StreamWriter sWriter = new StreamWriter(FILENAME);
XmlTextWriter writer = new XmlTextWriter(sWriter);
writer.WriteStartDocument();
writer.WriteStartElement("root");
writer.WriteStartElement("data");
double?[] attributes = new double?[] { null, 91.3467, 95.3052, 6.4722 };
XElement entry = new XElement("entry");
int index = 1;
foreach (double? attribute in attributes)
{
if (attribute == null)
{
entry.Add(new XAttribute("Attrib" + index++.ToString(), ""));
}
else
{
entry.Add(new XAttribute("Attrib" + index++.ToString(), attribute));
}
}
writer.WriteRaw(entry.ToString());
writer.WriteRaw(entry.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
writer.Close();
}
}
}
答案 2 :(得分:0)
使用对象序列化,然后您不必拥有构造映射代码的对象
using System.Xml.Serialization;
...
[XmlRoot("root")]
public class Example {
[XmlElement("data")]
public Entries Entries { get; set; }
}
public class Entries : List<List<string>>, IXmlSerializable {
public List<string> Attribs { get; set; }
public System.Xml.Schema.XmlSchema GetSchema() { return null; }
public void ReadXml(System.Xml.XmlReader reader) { reader.MoveToContent(); }
public void WriteXml(System.Xml.XmlWriter writer) {
foreach (var entry in this) {
writer.WriteStartElement("entry", "");
var label = 1;
foreach (var attrib in entry) {
writer.WriteAttributeString(string.Format("Attrib{0}", label), attrib);
label++;
}
writer.WriteEndElement();
}
}
}
...
var xml = new XmlSerializer(typeof(Example), "");
xml.Serialize(stream, example);