有没有办法在toString()函数中获取xml编码?
例:
xml.Save("myfile.xml");
导致
<?xml version="1.0" encoding="utf-8"?>
<Cooperations>
<Cooperation>
<CooperationId>xxx</CooperationId>
<CooperationName>Allianz Konzern</CooperationName>
<LogicalCustomers>
但
tb_output.Text = xml.toString();
导致像这样的输出
<Cooperations>
<Cooperation>
<CooperationId>xxx</CooperationId>
<CooperationName>Allianz Konzern</CooperationName>
<LogicalCustomers>
...
要么明确写出声明,要么使用StringWriter
并调用Save()
:
using System;
using System.IO;
using System.Text;
using System.Xml.Linq;
class Test
{
static void Main()
{
string xml = @"<?xml version='1.0' encoding='utf-8'?>
<Cooperations>
<Cooperation />
</Cooperations>";
XDocument doc = XDocument.Parse(xml);
StringBuilder builder = new StringBuilder();
using (TextWriter writer = new StringWriter(builder))
{
doc.Save(writer);
}
Console.WriteLine(builder);
}
}
您可以轻松地将其添加为扩展方法:
public static string ToStringWithDeclaration(this XDocument doc)
{
if (doc == null)
{
throw new ArgumentNullException("doc");
}
StringBuilder builder = new StringBuilder();
using (TextWriter writer = new StringWriter(builder))
{
doc.Save(writer);
}
return builder.ToString();
}
这样做的好处是,如果没有声明,它就不会爆炸:)
然后你可以使用:
string x = doc.ToStringWithDeclaration();
请注意,这将使用utf-16作为编码,因为这是StringWriter
中的隐式编码。您可以通过创建StringWriter
的子类来自己影响,例如: to always use UTF-8。
Declaration属性将包含XML声明。要获取内容和声明,您可以执行以下操作:
tb_output.Text = xml.Declaration.ToString() + xml.ToString()
用这个:
output.Text = String.Concat(xml.Declaration.ToString() , xml.ToString())
我确实喜欢这个
string distributorInfo = string.Empty;
XDocument distributors = new XDocument();
//below is important else distributors.Declaration.ToString() throws null exception
distributors.Declaration = new XDeclaration("1.0", "utf-8", "yes");
XElement rootElement = new XElement("Distributors");
XElement distributor = null;
XAttribute id = null;
distributor = new XElement("Distributor");
id = new XAttribute("Id", "12345678");
distributor.Add(id);
rootElement.Add(distributor);
distributor = new XElement("Distributor");
id = new XAttribute("Id", "22222222");
distributor.Add(id);
rootElement.Add(distributor);
distributors.Add(rootElement);
distributorInfo = String.Concat(distributors.Declaration.ToString(), distributors.ToString());
请参阅下面我在distributorInfo中获得的内容
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Distributors>
<Distributor Id="12345678" />
<Distributor Id="22222222" />
<Distributor Id="11111111" />
</Distributors>
与其他+1答案类似,但有关声明的更多详细信息,以及稍微更准确的连接。
<xml />
声明应该在格式化的XML中独立,所以我确保我们添加了换行符。注意:使用Environment.Newline
,它将生成平台特定的换行符
// Parse xml declaration menthod
XDocument document1 =
XDocument.Parse(@"<?xml version=""1.0"" encoding=""iso-8859-1""?><rss version=""2.0""></rss>");
string result1 =
document1.Declaration.ToString() +
Environment.NewLine +
document1.ToString() ;
// Declare xml declaration method
XDocument document2 =
XDocument.Parse(@"<rss version=""2.0""></rss>");
document2.Declaration =
new XDeclaration("1.0", "iso-8859-1", null);
string result2 =
document2.Declaration.ToString() +
Environment.NewLine +
document2.ToString() ;
两种结果都产生:
<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0"></rss>
您也可以使用XmlWriter并调用
Writer.WriteDocType()
方法。
其中一些答案解决了海报的要求,但似乎过于复杂。这是一个简单的扩展方法,它避免了需要单独的编写器,处理缺少的声明并支持标准的ToString SaveOptions参数。
public static string ToXmlString(this XDocument xdoc, SaveOptions options = SaveOptions.None)
{
var newLine = (options & SaveOptions.DisableFormatting) == SaveOptions.DisableFormatting ? "" : Environment.NewLine;
return xdoc.Declaration == null ? xdoc.ToString(options) : xdoc.Declaration + newLine + xdoc.ToString(options);
}
要使用扩展名,只需将xml.ToString()
替换为xml.ToXmlString()
即可
string uploadCode = "UploadCode";
string LabName = "LabName";
XElement root = new XElement("TestLabs");
foreach (var item in returnList)
{
root.Add(new XElement("TestLab",
new XElement(uploadCode, item.UploadCode),
new XElement(LabName, item.LabName)
)
);
}
XDocument returnXML = new XDocument(new XDeclaration("1.0", "UTF-8","yes"),
root);
string returnVal;
using (var sw = new MemoryStream())
{
using (var strw = new StreamWriter(sw, System.Text.UTF8Encoding.UTF8))
{
returnXML.Save(strw);
returnVal = System.Text.UTF8Encoding.UTF8.GetString(sw.ToArray());
}
}
// ReturnVal has the string with XML data with XML declaration tag