[英]How to add a root node to an 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");
}
}
我不知道通过序列化以这种方式附加对象的方法。 我知道的唯一替代方法是序列化对象数组。 看起来像:
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();
}
考虑将序列化器声明为静态(请阅读识别和防止托管代码中的内存泄漏以了解原因):
private static readonly XmlSerializer xs;
但是,如果您愿意使用Linq to 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");
感谢Arie提供的链接( XmlDocument的Serialise对象 ),您可以执行以下操作:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.