开发者

Output values of xml file in a text file with format

开发者 https://www.devze.com 2022-12-16 09:09 出处:网络
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 attribut

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

精彩评论

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