C#写入XML文档

  //XML文件写入数据库同步的基础数据以及XML设计格式

  internal void WriterInfo(string FilePath)

  {

  XmlDocument doc = new XmlDocument();

  XmlElement XML = doc.CreateElement("XML");

  XmlElement Conn = doc.CreateElement("Conn");

  Conn.SetAttribute("Name", "Conn1");

  Conn.SetAttribute("Type", "SQL Server数据库");

  XmlElement Source = doc.CreateElement("Source");

  XmlElement SServer = doc.CreateElement("SServer");

  XmlElement SIdentity = doc.CreateElement("SIdentity");

  XmlElement SDataBase = doc.CreateElement("SDataBase");

  XmlElement SUserID = doc.CreateElement("SUserID");

  XmlElement SPwd = doc.CreateElement("SPwd");

  XmlElement Target = doc.CreateElement("Target");

  XmlElement TServer = doc.CreateElement("TServer");

  XmlElement TIdentity = doc.CreateElement("TIdentity");

  XmlElement TDataBase = doc.CreateElement("TDataBase");

  XmlElement TUserID = doc.CreateElement("TUserID");

  XmlElement TPwd = doc.CreateElement("TPwd");

  doc.AppendChild(XML);

  XML.AppendChild(Conn);

  Conn.AppendChild(Source);

  Source.AppendChild(SServer);

  SServer.InnerText = "123.123.123";

  Source.AppendChild(SIdentity);

  Source.AppendChild(SDataBase);

  Source.AppendChild(SUserID);

  Source.AppendChild(SPwd);

  Conn.AppendChild(Target);

  Target.AppendChild(TServer);

  TServer.InnerText = "00000000";

  Target.AppendChild(TIdentity);

  Target.AppendChild(TDataBase);

  Target.AppendChild(TUserID);

  Target.AppendChild(TPwd);

  XmlWriterSettings settings = new XmlWriterSettings();

  settings.Indent = true;

  settings.Encoding = Encoding.UTF8;

  XmlWriter writer = XmlWriter.Create(FilePath, settings);

  doc.WriteTo(writer);

  writer.Flush();

  writer.Close();

  MessageBox.Show("It's Ok!");

  }

相关文章