如何使用 XMLWriterClass 在 C# 中生成以下 Xml 代码?
How do I generate the following Xml code in C# with XMLWriterClass?
我正在尝试获取这个:
<ATCWaypointEnd id="KLKP">
</ATCWaypointEnd>
但我明白了:
<ATCWaypointEnd id="KLKP"></ATCWaypointEnd>
这是我的代码:
writer.WriteStartElement("ATCWaypointEnd");
writer.WriteStartAttribute("id");
writer.WriteString(ICAO);
writer.WriteFullEndElement();
我建议创建一个 class 来表示您的 XML 文件,然后使用序列化。这样你就可以让框架创建 XML 元素,你可以很容易地指定它是否应该包含换行符(缩进)。
您还可以使用外部工具生成您的 POCO classes,例如:https://xmltocsharp.azurewebsites.net/
使用您提供的 xml 片段:
// The class representing the XML file
[XmlRoot(ElementName="ATCWaypointEnd")]
public class ATCWaypointEnd
{
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
}
// The method that will return the object as a XML string
public static string GenerateXml(ATCWaypointEnd obj)
{
string xml;
using (var stream = new MemoryStream())
{
var serializer = new XmlSerializer(typeof(ATCWaypointEnd));
var writer = new XmlTextWriter(stream, encoding);
writer.Formatting = Formatting.Indented; // Here
serializer.Serialize(writer, obj);
xml = encoding.GetString(stream.ToArray());
}
return xml;
}
然后在你的代码中你可以这样使用:
var obj = new ATCWaypointEnd();
obj.Id = "KLKP";
var xml = GenerateXml(obj);
您可以执行以下操作:
writer.WriteStartElement("ATCWaypointEnd");
writer.WriteAttributeString("id", ICAO);
writer.WriteString(System.Environment.NewLine);
writer.WriteFullEndElement();
完整代码见下方:
添加以下“using”语句:
using System.Xml;
using System.IO;
选项 1:
private void WriteXmlData(string ICAO, string filename)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = false;
settings.NewLineChars = Environment.NewLine;
string dirName = System.IO.Path.GetDirectoryName(filename);
if (!System.IO.Directory.Exists(dirName))
{
System.Diagnostics.Debug.WriteLine("Output folder doesn't exist. Creating.");
//create folder if it doesn't exist
System.IO.Directory.CreateDirectory(dirName);
}
using (XmlWriter writer = XmlWriter.Create(filename, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("ATCWaypointEnd");
writer.WriteAttributeString("id", ICAO);
writer.WriteString(System.Environment.NewLine);
writer.WriteFullEndElement();
writer.WriteEndDocument();
writer.Close();
writer.Dispose();
}
}
用法:
WriteXmlData("KLKP", @"C:\Temp\test.xml");
选项 2:
private string WriteXmlData(string ICAO)
{
string xmlOutput = string.Empty;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = false;
settings.NewLineChars = Environment.NewLine;
using (MemoryStream ms = new MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create(ms, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("ATCWaypointEnd");
writer.WriteAttributeString("id", ICAO);
writer.WriteString(System.Environment.NewLine);
writer.WriteFullEndElement();
writer.WriteEndDocument();
writer.Close();
writer.Dispose();
}
//reset position
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
//put XML into string
xmlOutput = sr.ReadToEnd();
//clean up
sr.Close();
sr.Dispose();
}
//clean up
ms.Close();
ms.Dispose();
}
return xmlOutput;
}
用法:
string output = WriteXmlData("KLKP");
我正在尝试获取这个:
<ATCWaypointEnd id="KLKP">
</ATCWaypointEnd>
但我明白了:
<ATCWaypointEnd id="KLKP"></ATCWaypointEnd>
这是我的代码:
writer.WriteStartElement("ATCWaypointEnd");
writer.WriteStartAttribute("id");
writer.WriteString(ICAO);
writer.WriteFullEndElement();
我建议创建一个 class 来表示您的 XML 文件,然后使用序列化。这样你就可以让框架创建 XML 元素,你可以很容易地指定它是否应该包含换行符(缩进)。
您还可以使用外部工具生成您的 POCO classes,例如:https://xmltocsharp.azurewebsites.net/
使用您提供的 xml 片段:
// The class representing the XML file
[XmlRoot(ElementName="ATCWaypointEnd")]
public class ATCWaypointEnd
{
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
}
// The method that will return the object as a XML string
public static string GenerateXml(ATCWaypointEnd obj)
{
string xml;
using (var stream = new MemoryStream())
{
var serializer = new XmlSerializer(typeof(ATCWaypointEnd));
var writer = new XmlTextWriter(stream, encoding);
writer.Formatting = Formatting.Indented; // Here
serializer.Serialize(writer, obj);
xml = encoding.GetString(stream.ToArray());
}
return xml;
}
然后在你的代码中你可以这样使用:
var obj = new ATCWaypointEnd();
obj.Id = "KLKP";
var xml = GenerateXml(obj);
您可以执行以下操作:
writer.WriteStartElement("ATCWaypointEnd");
writer.WriteAttributeString("id", ICAO);
writer.WriteString(System.Environment.NewLine);
writer.WriteFullEndElement();
完整代码见下方:
添加以下“using”语句:
using System.Xml;
using System.IO;
选项 1:
private void WriteXmlData(string ICAO, string filename)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = false;
settings.NewLineChars = Environment.NewLine;
string dirName = System.IO.Path.GetDirectoryName(filename);
if (!System.IO.Directory.Exists(dirName))
{
System.Diagnostics.Debug.WriteLine("Output folder doesn't exist. Creating.");
//create folder if it doesn't exist
System.IO.Directory.CreateDirectory(dirName);
}
using (XmlWriter writer = XmlWriter.Create(filename, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("ATCWaypointEnd");
writer.WriteAttributeString("id", ICAO);
writer.WriteString(System.Environment.NewLine);
writer.WriteFullEndElement();
writer.WriteEndDocument();
writer.Close();
writer.Dispose();
}
}
用法:
WriteXmlData("KLKP", @"C:\Temp\test.xml");
选项 2:
private string WriteXmlData(string ICAO)
{
string xmlOutput = string.Empty;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = false;
settings.NewLineChars = Environment.NewLine;
using (MemoryStream ms = new MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create(ms, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("ATCWaypointEnd");
writer.WriteAttributeString("id", ICAO);
writer.WriteString(System.Environment.NewLine);
writer.WriteFullEndElement();
writer.WriteEndDocument();
writer.Close();
writer.Dispose();
}
//reset position
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
//put XML into string
xmlOutput = sr.ReadToEnd();
//clean up
sr.Close();
sr.Dispose();
}
//clean up
ms.Close();
ms.Dispose();
}
return xmlOutput;
}
用法:
string output = WriteXmlData("KLKP");