2013-02-04 97 views
1

XML文件我的代碼被生成如下如何將一個根節點添加到xml中?

<?xml version="1.0"?> 
<DataClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <pathI>D:\POC\Input\2</pathI> 
    <pathO>D:\POC\Output</pathO> 
    <prefix>2_</prefix> 
    <frequency>25</frequency> 
</DataClass><?xml version="1.0"?> 
<DataClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <pathI>D:\POC\Input\3</pathI> 
    <pathO>D:\POC\Output</pathO> 
    <prefix>3_</prefix> 
    <frequency>33</frequency> 
</DataClass> 

欲一個根元素添加到XML,這樣我可以進一步使用xml來填充數據網格視圖。 如果可能的話也想從每個節點消除標籤.. 需要幫助

DataClass data = new DataClass(); 
data.pathI = txt_input.Text; 
data.pathO = txt_Output.Text; 
data.frequency = Convert.ToInt32(txt_freq.Text); 
data.prefix = txt_prefix.Text; 
XmlDocument doc = new XmlDocument(); 


XmlSerializer xs = new XmlSerializer(typeof(DataClass)); 
if (!File.Exists("Data.xml")) 
{ 
    using (FileStream fs = new FileStream("Data.xml", FileMode.Create)) 
    { 
     xs.Serialize(fs, data); 
     fs.Close(); 
     fs.Dispose(); 
     MessageBox.Show("Data loaded to the xml"); 
    } 
} 
else if (File.Exists("Data.xml")) 
{ 
    using (FileStream fs = new FileStream("Data.xml",FileMode.Append)) 
    { 
      xs.Serialize(fs, data); 
      fs.Close(); 
      fs.Dispose(); 
      MessageBox.Show("Data loaded to the xml"); 
    } 
} 
+0

消除每一個節點 – user2038650

+0

所以標籤 ,請向我們展示您的代碼,即生成此xml。 – horgh

+0

編輯的問題.. plzz檢查.. – user2038650

回答

1

我不知道一種方法,通過系列化這種方式追加對象。我知道的唯一選擇是序列化一個對象數組。這看起來像:

DataClass[] objects = ...//get all your objects 
if(xs == null) 
{ 
    xs = new XmlSerializer(typeof(DataClass[]), 
          new XmlRootAttribute("Your root name")); 
} 
using (FileStream fs = new FileStream("Data.xml", FileMode.Create)) 
{ 
    xs.Serialize(fs, data); 
    fs.Close(); 
} 

考慮宣佈串行靜態(讀Identify And Prevent Memory Leaks In Managed Code理解爲什麼):

private static readonly XmlSerializer xs; 

不過,如果你打開使用LINQ而不是XML,你可以獲得你需要的功能。但是,每次需要修改xml時,都必須將整個xml加載到內存中。

XElement x; 
if (File.Exists("Data.xml")) 
    x = XElement.Load("Data.xml"); 
else 
    x = new XElement("Data"); 
x.Add(new XElement("DataClass", 
        new XElement("pathI", @"D:\POC\Input\2"), 
        new XElement("pathO", @"D:\POC\Output"), 
        new XElement("prefix", "2_"), 
        new XElement("frequency", "25"))); 
x.Save("Data.xml"); 

多虧了阿里(Serialise object to XmlDocument)給出的鏈接,你可以做到以下幾點:

XmlDocument temp = new XmlDocument(); //create a temporary xml document 
var navigator = temp.CreateNavigator(); //use its navigator 
using (var w = navigator.AppendChild()) //to get an XMLWriter 
    xs.Serialize(w, data);    //serialize your data to it 

XmlDocument xdoc = new XmlDocument(); //init the main xml document 
string filename = "Data.xml"; 
if (File.Exists(filename))    //if file exists 
    xdoc.Load(filename);    //load xml from it 
else         //or 
{ 
    //add xml declaration to the top of the new xml document 
    xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0", "utf-8", null)); 
    //create the root element 
    xdoc.AppendChild(xdoc.CreateElement("Data")); 
} 

var newchild = xdoc.CreateElement("DataClass"); //the new element 
newchild.InnerXml = temp.FirstChild.InnerXml; //copy the serialized content 

//append the new element to the root 
xdoc.ChildNodes[1].AppendChild(newchild);  
//save the document 
xdoc.Save(filename); 
+0

dis似乎沒有在我的代碼工作..嘗試添加XMLRootAttribute,但它辛苦工作:( – user2038650

+0

@ user2038650我編輯了我的答案...請檢查它 – horgh