Output values of xml file in a text file with format
How can I make a C# console program reads开发者_StackOverflow社区 the attributes of an xml file then output it to a text file in the format: textbox.Settings.Keywords.Add("attribute")
where attribute is the attribute. A sample of the xml file:
<Keywords>
...
<Keyword name = "if" />
<Keyword name = "else" />
...
</Keywords>
Like this:
File.WriteAllLines(
XElement.Load(filename)
.Descendants("Keyword")
.Attributes("name")
.Select(n => "textbox.Settings.Keywords.Add(\"" + n.Value + "\");")
.ToArray()
);
Try this:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("...");
using(StreamWriter writer = new StreamWriter("yourfile.txt"))
foreach (XmlNode node in xmlDoc.SelectNodes("//Element/@*"))
{
writer.WriteLine(
String.Format("textbox.Settings.Keywords.Add(\"{0}\")",
node.Name));
}
精彩评论