1

私はこのような LINQ クエリを持っています。自動的に追加される XML 宣言タグを削除する必要があります。

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 を文字列として返す必要があるだけです。

4

2 に答える 2

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();
}
于 2013-07-16T08:47:10.667 に答える
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宣言しても表示されません。

于 2013-07-16T10:50:50.417 に答える