我正在尝试做两件事:
获取格式化的输出XML 使用TABULATION而不是空格。
删除它生成的结束空间 视频元素。
" />
到
"/>
我尝试过使用
xmlWriter.Formatting = Formatting.Indented;
以及
IndentChar
但他们没有为我工作,不知道为什么。
这是我目前的代码,我也想听听有关改进的建议和建议:
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8' standalone='yes'");
xmlWriter.WriteComment(@" This file was made by @author");
xmlWriter.WriteStartElement("videos");
xmlWriter.Close();
xmlDoc.Load(filename);
XmlNode root = xmlDoc.DocumentElement;
foreach (int myID in ExportListIDs)
{
XmlElement video = xmlDoc.CreateElement("video");
root.AppendChild(video);
video.SetAttribute("videoID", myID.ToString());
}
xmlDoc.Save(filename);
答案 0 :(得分:1)
我已设法使用以下代码解决问题1,但我仍然不知道是否可以在元素问题2的末尾删除"
和/>
之间的空格
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writeXML = XmlWriter.Create("test.xml", settings);
writeXML.WriteStartDocument();
writeXML.WriteComment(@" This file was made by @author");
writeXML.WriteStartElement("videos");
foreach (var item in myList)
{
writeXML.WriteStartElement("video");
writeXML.WriteAttributeString("ID", item.Key.ToString());
writeXML.WriteAttributeString("Name", item.Value);
writeXML.WriteStartElement("object");
writeXML.WriteAttributeString("A", item.Key.ToString());
writeXML.WriteAttributeString("B", item.Value);
writeXML.WriteEndElement();
writeXML.WriteEndElement();
}
writeXML.WriteEndElement();
writeXML.WriteEndDocument();
writeXML.Close();