Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#XML系列化&;追加新数据_C#_Xml - Fatal编程技术网

C#XML系列化&;追加新数据

C#XML系列化&;追加新数据,c#,xml,C#,Xml,我正在尝试创建一个具有以下结构的XML文件 然而,这是我得到的(一行): 如何添加新数据?不要每次都创建新的XML。添加一些设置以实现您试图在XML文件中实现的缩进: //I added this part (XmlWriterSettings): XmlWriterSettings mySettings = new XmlWriterSettings(); mySettings.Indent = true; mySettings.IndentChars = (" "); my

我正在尝试创建一个具有以下结构的XML文件

然而,这是我得到的(一行):


如何添加新数据?不要每次都创建新的XML。

添加一些设置以实现您试图在XML文件中实现的缩进:

 //I added this part (XmlWriterSettings):
 XmlWriterSettings mySettings = new XmlWriterSettings();
 mySettings.Indent = true;
 mySettings.IndentChars = ("    ");
 mySettings.CloseOutput = true;
 mySettings.OmitXmlDeclaration = true;

//Added the settings you intialize and set on your XmlWriter:
 XmlWriter writer = XmlWriter.Create(@"C:\Users\Alek\Dropbox\D\Debug\product.xml", mySettings);

 writer.WriteStartDocument();

 writer.WriteComment("This file is generated by the program.");

 writer.WriteStartElement("root");
 writer.WriteStartElement("OFBM");
 writer.WriteAttributeString("time", "9:15");
 writer.WriteAttributeString("date", "22.06.2016");
 writer.WriteElementString("folder", @"file:///C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client");
 writer.WriteElementString("folder", @"file:///C:/Arduino223");
 writer.WriteEndElement();
 //writer.WriteEndElement();
 //writer.WriteEndDocument();
  writer.Flush();
  writer.Close();
要附加新数据而不重新创建文件,可以执行以下操作:

 XDocument doc = XDocument.Load(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");
 XElement school = doc.Element("root");
 school.Add(new XElement("OFBM",
                   new XElement("folder", "file:///C:/Arduino223")));
 doc.Save(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");
看看这篇文章。有一种添加缩进的解决方案。第二个问题:-如何将xml添加到现有文件中。
 //I added this part (XmlWriterSettings):
 XmlWriterSettings mySettings = new XmlWriterSettings();
 mySettings.Indent = true;
 mySettings.IndentChars = ("    ");
 mySettings.CloseOutput = true;
 mySettings.OmitXmlDeclaration = true;

//Added the settings you intialize and set on your XmlWriter:
 XmlWriter writer = XmlWriter.Create(@"C:\Users\Alek\Dropbox\D\Debug\product.xml", mySettings);

 writer.WriteStartDocument();

 writer.WriteComment("This file is generated by the program.");

 writer.WriteStartElement("root");
 writer.WriteStartElement("OFBM");
 writer.WriteAttributeString("time", "9:15");
 writer.WriteAttributeString("date", "22.06.2016");
 writer.WriteElementString("folder", @"file:///C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client");
 writer.WriteElementString("folder", @"file:///C:/Arduino223");
 writer.WriteEndElement();
 //writer.WriteEndElement();
 //writer.WriteEndDocument();
  writer.Flush();
  writer.Close();
 XDocument doc = XDocument.Load(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");
 XElement school = doc.Element("root");
 school.Add(new XElement("OFBM",
                   new XElement("folder", "file:///C:/Arduino223")));
 doc.Save(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");