想CSV转换为XML,使用此代码:C#的DataContractSerializer:转换.csv格式.XML,无标签
// test person
var person = new Person("Jim", "TestJob", 1000);
var fileStream = new FileStream("sample.xml", FileMode.Create);
var writer = XmlDictionaryWriter.CreateTextWriter(fileStream);
var dcs = new DataContractSerializer(typeof(Person));
// Use the writer to start a document.
writer.WriteStartDocument(true);
// Use the writer to write the root element.
writer.WriteStartElement("Company");
// Use the writer to write an element.
writer.WriteElementString("Name", "Microsoft");
// Use the serializer to write the start,
// content, and end data.
dcs.WriteObject(writer, person);
// Use the writer to write the end element and
// the end of the document
writer.WriteEndElement();
writer.WriteEndDocument();
// Close and release the writer resources.
writer.Flush();
fileStream.Flush();
fileStream.Close();
添加一些标签后,生成的代码似乎也是正确的! 我应该忽略它吗?这可以做到吗?已经搜索过,但没有太多发现关于标签本身。
有没有你正在使用'XmlDictionaryWriter'而不是'XmlWriter'理由吗? 'XmlWriter.Create'允许您传入具有缩进属性的设置,您将其设置为true来强制缩进。 –