我有这样的LINQ查询,我需要删除自动添加的XML Declaration标记。
var cubbingmessagexml = new XDocument(
new XElement("MESSAGE", new XAttribute("ID", "CUB"),
new XElement("RECORD", new XAttribute("STORENO", cubing.StoreID),
new XAttribute("TPNB", cubing.ProductCode),
new XAttribute("QUANTITY", cubing.Quantity),
new XAttribute("CUBINGTIME", cubing.CubingDateTime.ToString("yyyyMMddHHmmss")),
new XAttribute("SHELFFACING", cubing.ShelfFacing)
)));
xml = cubbingmessagexml.ToString();
请帮助
我不想保存XML文件,只需要将XML作为字符串返回
答案 0 :(得分:2)
如果您在顶部引用xml版本和内容,则会有一个xml编写器设置将其关闭。
var writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
using (var buffer = new StringWriter())
using (var writer = XmlWriter.Create(buffer, writerSettings))
{
cubbingmessagexml.Save(writer);
writer.Flush();
string result = buffer.ToString();
}
答案 1 :(得分:1)
<击>
跳过XDocument
:
var cubbingmessagexml =
new XElement("MESSAGE", new XAttribute("ID", "CUB"),
new XElement("RECORD",
new XAttribute("STORENO", cubing.StoreID),
new XAttribute("TPNB", cubing.ProductCode),
new XAttribute("QUANTITY", cubing.Quantity),
new XAttribute("CUBINGTIME", cubing.CubingDateTime.ToString("yyyyMMddHHmmss")),
new XAttribute("SHELFFACING", cubing.ShelfFacing)
)
);
xml = cubbingmessagexml.ToString();
来自MSDN:
请注意,如果需要XDocument类提供的特定功能,则只需创建XDocument对象。在许多情况下,您可以直接使用XElement。直接使用XElement是一种更简单的编程模型。
和
击><击>
如前所述,XElement类是LINQ to XML编程接口中的主类。在许多情况下,您的应用程序不需要您创建文档。通过使用XElement类,您可以创建XML树,向其添加其他XML树,修改XML树并保存它。 击>
即使使用XDocument
,也不会显示声明。