开发者

Dynamically create text file from xml/text file

Can I dynamically create a text file from a c# program, using data from a previously created xml file and text file, I have written half the code, but can't go any further. Please help

using System;
using System.IO;
using System.Xml;

namespace Task3
{

   class TextFileReader
   {

      static void Main(string[] args)
      {
         String strn=" ", strsn=String.Empty;  
         XmlTextReader reader = new XmlTextReader("my.xml");

                 while (reader.Read())
                 {
                    switch (reader.NodeType)
                    {
                       case XmlNodeType.Element: // The node is an element.
                          if (reader.HasAttributes)
                          {
                             strn = reader.GetAttribute(0);
                             strsn = reader.GetAttribute(1);
                             int counter = 0;
                             string line;
                             // Read the file and display it line by line.
                             System.IO.StreamReader file = new System.IO.StreamReader("read_file.txt");
                             string ch, ch1;
                             while ((line = file.ReadLine()) != null)
                             {
                                if (line.Substring(0, 1).Equals("开发者_StackOverflow中文版%"))
                                {
                                   int a = line.IndexOf('%');
                                   int b = line.LastIndexOf('%');
                                   ch = line.Substring(a + 1, b - 1);
                                   ch1 = line.Substring(a, b+1);
                                   if (ch == "name")
                                   {
                                      string test = line.Replace(ch1, strn);
                                      Console.WriteLine(test);
                                   }
                                   else if (ch == "sirname")
                                   {
                                      string test = line.Replace(ch1, strsn);
                                      Console.WriteLine(test);
                                   }
                                }
                                else
                                {
                                   Console.WriteLine(line);
                                }
                                counter++;
                             }
                             file.Close();
                          }
                          break;
                    }

                 }        

         // Suspend the screen.
         Console.ReadLine();
      }
   }
}

The xml file from which I am reading is:

<?xml version="1.0" encoding="utf-8" ?> 
<Workflow>
  <User UserName="pqr" Sirname="sbd" /> 
  <User UserName="abc" Sirname="xyz" /> 
</Workflow>

And the text file is:

hi this is me
%sirname%
%name%

But this is not what I want..please help


Have you considered using XSLT instead? It is easy to define an .xsl file which 'translates' your XML into a formatted text file, which seems to be what you want to do.


I think, what you actually want to do is replacing tokens like %sirname% with data from the xml. You already succeeded to read the xml and write the text file. So your question remains: how to replace the tokens between %-symbols?

I suggest to use regular expression. See this explenation, ask more specific questions if you get stuck.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜