0

次の形式の XML があります

<Attachment>
 <AttachmentName>Top Nav Menu.docx</AttachmentName>
 <Subject>Attachment1</Subject>
 <Sender>JameelM@orioninc.com</Sender>
 </Attachment>

添付ファイルを閉じるノードの後に​​、上記のように別の添付ファイルを添付したいと考えています。以下は、xmlファイルを書くために私が書いたコードです

 var doc = new XDocument(

                new XDeclaration("1.0", "utf-16", "true"),

                new XProcessingInstruction("test", "value"),

                new XElement("Attachment",new XElement("AttachmentName", attachment.Name),
                                          new XElement("Subject", exchangeEmailInformation.Subject),
                                          new XElement("Sender", exchangeEmailInformation.Sender
                                          )));
            doc.Save(ConfigInformation.BackUpPath + FolderId[index]+"\\Attachments"+index+".xml");
4

2 に答える 2

3

添付ファイルのルート ノードを作成します。

var doc = new XDocument(
    new XDeclaration("1.0", "utf-16", "true"),
    new XProcessingInstruction("test", "value"),
    new XElement("Attachments", 
        new XElement("Attachment", 
            new XElement("AttachmentName", attachment.Name),
            new XElement("Subject", exchangeEmailInformation.Subject),
            new XElement("Sender", exchangeEmailInformation.Sender)
    )));

別の添付ファイルを追加する場合は、ドキュメントをロードして添付ファイルをルートに追加します。

doc.Root.Add(new XElement("Attachment",
    new XElement("AttachmentName", attachment.Name),
    new XElement("Subject", exchangeEmailInformation.Subject),
    new XElement("Sender", exchangeEmailInformation.Sender)
));
于 2012-12-24T14:44:25.043 に答える
0

XMLSerializerクラスを使用します。そこでは、XMLファイルをクラスであるかのように処理できます。見てください、あなたはそれを好きになるでしょう:)

XMLのロード->コードでクラスを使用(変更、削除、追加)->XMLにシリアル化して戻す

于 2012-12-24T22:29:15.860 に答える