开发者

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));
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜