C# ToString()删除XML编码标记
有没有办法在toString()函数中获取xml编码 示例:C# ToString()删除XML编码标记,c#,linq-to-xml,C#,Linq To Xml,有没有办法在toString()函数中获取xml编码 示例: xml.Save("myfile.xml"); 导致 <?xml version="1.0" encoding="utf-8"?> <Cooperations> <Cooperation> <CooperationId>xxx</CooperationId> <CooperationName>Allianz Konzern</Coope
xml.Save("myfile.xml");
导致
<?xml version="1.0" encoding="utf-8"?>
<Cooperations>
<Cooperation>
<CooperationId>xxx</CooperationId>
<CooperationName>Allianz Konzern</CooperationName>
<LogicalCustomers>
导致这样的输出
<Cooperations>
<Cooperation>
<CooperationId>xxx</CooperationId>
<CooperationName>Allianz Konzern</CooperationName>
<LogicalCustomers>
...
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());
xxx
安联康泽酒店
...
声明属性将包含XML声明。要获取内容加声明,可以执行以下操作:
tb_output.Text = xml.Declaration.ToString() + xml.ToString()
您还可以使用XmlWriter并调用
Writer.WriteDocType()
方法。或者显式写出声明,或者使用
StringWriter
并调用Save()
:
这样做的好处是,如果没有声明,它不会爆炸:)
然后您可以使用:
string x = doc.ToStringWithDeclaration();
请注意,这将使用utf-16作为编码,因为这是StringWriter
中的隐式编码。您可以通过创建StringWriter
的子类来影响这一点,例如..使用以下方法:
output.Text = String.Concat(xml.Declaration.ToString() , xml.ToString())
我确实喜欢这个
<Cooperations>
<Cooperation>
<CooperationId>xxx</CooperationId>
<CooperationName>Allianz Konzern</CooperationName>
<LogicalCustomers>
...
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中自己的一行,所以我要确保添加了换行符。
注意:使用Environment.Newline
,因此它将生成特定于平台的新行
//解析xml声明方法
XDocument文档1=
XDocument.Parse(@“”);
字符串结果1=
document1.Declaration.ToString()+
环境新线+
文档1.ToString();
//声明xml声明方法
XDocument document2=
XDocument.Parse(@“”);
文件2.声明=
新的XDeclaration(“1.0”,“iso-8859-1”,空);
字符串结果2=
document2.Declaration.ToString()+
环境新线+
文档2.ToString();
这两个结果产生:
其中一些答案解决了海报的要求,但似乎过于复杂。下面是一个简单的扩展方法,它避免了使用单独的编写器,处理丢失的声明,并支持标准的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()
似乎如果您在xdocument中不使用新的XDeclaration(“1.0”、“utf-8”、“yes”),这会创建一个错误,因为xml.Declaration为null。但是xml.save似乎可以自动检测正确的编码。或者,tb_output.Text=@“+xml代码>或…=$“{xdoc.Declaration}{Environment.NewLine}{xdoc}”代码>这有一个小问题,在执行保存时,XDocument声明中的编码被忽略并替换为StringWriter的编码,当您将扩展方法与:Utf8StringWriter from;)组合时,这可能是您想要的,也可能不是您想要的发现使用上面的扩展方法更容易,但返回以下内容。。。返回单据声明+单据ToString();如果声明为null,则只会产生一个空字符串。奇怪,但我现在无法让它工作()-它总是使用“utf-16”编码。我查看了XDocument.Save(TextWriter)
实现,它只是忽略了声明的编码,而不是XDocument.Save(String)
或XDocument.Save(Stream)
实现。我想知道为什么…@IlyaLuzyanin:是的,当您传入StringWriter
时,它将使用“utf-16”作为编码,除非您使用的编码覆盖编码
属性。我有另一个答案。我想你是说它完全放弃了“编码”…没有创建新的XDeclaration(“1.0”、“utf-8”、“yes”)并添加到XDocument或其他对象中,xml.Declaration.ToString()将抛出一个空异常。它更安全,因为Concat不关心空字符串:output.Text=String.Concat(xml.Declaration,xml)很好的例子。一些注意事项:1)使用新的XDeclaration(“1.0”、“utf-8”)代替新的XDeclaration(“1.0”、“utf-8”、“是”),2)在最后一行插入新行:distributors.Declaration.ToString()+Environment.NewLine+distributors.ToString()
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);
}
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