1

我的代码生成的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");
     }
}
4

1 回答 1

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();
}

考虑将序列化程序声明为静态(阅读识别并防止托管代码中的内存泄漏以了解原因):

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 提供的链接(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);
于 2013-02-04T08:32:03.960 回答