开发者

How to write inside an xml file using xmldocument

How can I write inside an xml file using c# and the xmldocument or xpathnavigator class. I want to write a node inside of three entries. As an example, I want to write a "window" entry a few times..

<tabpage>
      <form>
         <Window>
             <.....>name<...&开发者_如何转开发gt;
             <.....>age<.....>
             <.....>gender<..>
         </Window>
         <Window>
             <.....>name<...>
             <.....>age<.....>
             <.....>gender<..>
         </Window>
      </form>
 <tabpage>              


you can use something like the following:

string targetFileName = "";
string[] myWindows = new string[];

using (XmlTextWriter oXmlTextWriter = new XmlTextWriter(targetFileName , Encoding.UTF8))
{
  oXmlTextWriter.Formatting = Formatting.Indented;
  oXmlTextWriter.WriteStartDocument();
  oXmlTextWriter.WriteStartElement("tabpage");
  oXmlTextWriter.WriteStartElement("form");

  foreach( string window in myWindows)
  {
     oXmlTextWriter.WriteStartElement("Window");

     oXmlTextWriter.WriteElementString("name", nameValue);
     oXmlTextWriter.WriteElementString("age", nameValue);
     oXmlTextWriter.WriteElementString("gender", nameValue);

     oXmlTextWriter.WriteEndElement(); // closing Window tag
  }
  oXmlTextWriter.WriteEndElement(); // closing form tag
  oXmlTextWriter.WriteEndElement(); // closing tabpage tag

  // closing the XML file
  oXmlTextWriter.WriteEndDocument();
  oXmlTextWriter.Flush();
  oXmlTextWriter.Close();
}

obviously you should change the above code snippet to fit to your specific XML you want to generate. you also didn't provide much information what should be inside a window XML element - do you plan to write XML attributes per window or sub XML elements describing name/age/gender information.

you can see many more examples in MSDN - go to http://msdn.microsoft.com and search from XmlTextWriter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜