簡體   English   中英

如何向 C# 中的數據集 Writexml 生成的 XML 添加額外信息?

[英]How do I add extra information to XML generated from a dataset Writexml in C#?

ds.WriteXml(strXmlTestCasePath, XmlWriteMode.IgnoreSchema); 

ds 是一個dataset 我想在這個 XML 中添加額外的行或額外的信息。 我該怎么做?

使用XmlWriter編寫您的DataSet 然后,您可以使用相同的 object 來編寫額外的 XML。

說明性代碼:

            System.Data.DataSet ds;
            System.Xml.XmlWriter x;
            ds.WriteXml(x);
            x.WriteElementString("test", "value");

您不能簡單地將更多 XML 寫入序列化DataSet的末尾,因為如果這樣做,您將生成一個包含多個頂級元素的 XML 文檔。 使用XmlWriter ,您需要執行以下操作:

using (XmlWriter xw = XmlWriter.Create(strXmlTestCasePath));
{
   xw.WriteStartElement("container");
   ds.WriteXml(xw, XmlWriteMode.IgnoreSchema);
   // from here on, you can use the XmlWriter to add XML to the end; you then
   // have to wrap things up by closing the enclosing "container" element:
   ...
   xw.WriteEndElement();
}

但是,如果您嘗試序列化的DataSet中添加 XML 元素,這對您沒有幫助。 為此,您需要序列化DataSet ,將其讀入XmlDocument ,然后使用 DOM 方法來操作 XML。

或者,也可以在序列化DataSet之前創建並填充一個新的DataTable ,然后在完成后將其刪除。 這實際上取決於您的實際要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM