如何将根节点添加到 xml

  • 本文关键字:xml 添加 根节点 c# 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:POCInput2</pathI>
  <pathO>D:POCOutput</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:POCInput3</pathI>
  <pathO>D:POCOutput</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:POCInput2"),
                    new XElement("pathO", @"D:POCOutput"),
                    new XElement("prefix", "2_"),
                    new XElement("frequency", "25")));
x.Save("Data.xml");

由于Arie提供的链接(将对象序列化为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);

最新更新