locked
Convert text file to xml file

    Question

  • Hello, I want to convert the text file to the xml file. Thanks for help.

    EXODUS
    
    1:1: Now these are the names of the children of Israel, which came into Egypt; every man and his household came with Jacob.
    1:2: Reuben, Simeon, Levi, and Judah, 
    1:3: Issachar, Zebulun, and Benjamin,
    1:4: Dan, and Naphtali, Gad, and Asher. 
    1:5: And all the souls that came out of the loins of Jacob were seventy souls: for Joseph was in Egypt already. 
    1:6: And Joseph died, and all his brethren, and all that generation.
    
    

    The xml format:

    <book name="Exodus">
    	<chapter number="1">
    		<verse number="1">Now these are the names of the sons of Israel, who came into Egypt (every man and his household came with Jacob):</verse>
    		<verse number="2">Reuben, Simeon, Levi, and Judah,</verse>
    		<verse number="3">Issachar, Zebulun, and Benjamin,</verse>
    		<verse number="4">Dan and Naphtali, Gad and Asher.</verse>
    		<verse number="5">All the souls who came out of the Jacob`s body were seventy souls, and Joseph was in Egypt already.</verse>
    		<verse number="6">Joseph died, as did all his brothers, and all that generation.</verse>
    

    Thanks.

     

     

     

    Monday, June 13, 2011 1:48 AM

Answers

  • Hi, Here is the code to read your text file and create xml ... 

    before the code a few assumptions....

    1. First line in the file is always a book name.

    2. 1:1 in this first digit is chapter number, second digit is verse number.

     

     class Program
     {
      #region Private Methods
    
      static void Main(string[] args)
      {
       Book book = new Book();
       List<string> allLines = ReadTextFile(@"C:\Users\kmahamuni\Desktop\Book.txt");
    
       ProcessLines(allLines, out book);
      }
    
      private static void ProcessLines(List<string> allLines, out Book book)
      {
       int lineNumber = 1;
       Book newBook = new Book();
       string[] lineSections = null;
       int currentChapter = 0;
    
       foreach (string line in allLines)
       {
        if (lineNumber == 1)
        {
         newBook.Name = line;
         newBook.chapter = new Chapter();
        }
        else
        {
         lineSections = line.Split(':');
    
         if (lineSections.Length > 1)
         {
          if (currentChapter != Convert.ToInt32(lineSections[0]))
          {
           Chapter chapter = new Chapter();
           currentChapter = Convert.ToInt32(lineSections[0]);
           chapter.Number = currentChapter;
           newBook.chapter = chapter;
           newBook.chapter.Verse = new List<Verse>();
          }
    
          Verse verse = new Verse();
          verse.Number = Convert.ToInt32(lineSections[1]);
          verse.Value = lineSections[2];
          newBook.chapter.Verse.Add(verse);
         }
        }
        lineNumber++;
       }
    
       book = newBook;
    
       string xml = book.ToXML();
      }
    
      private static List<string> ReadTextFile(string fileName)
      {
       if (File.Exists(fileName))
       {
        List<string> lines = new List<string>();
        string line = string.Empty;
    
        using (StreamReader reader = new StreamReader(fileName))
        {
         while ((line = reader.ReadLine()) != null)
         {
          lines.Add(line);
         }
        }
        return lines;
       }
       return null;
      }
    
      #endregion Private Methods
     }
    
     [Serializable]
     [XmlRoot(ElementName = "Book", Namespace = null)]
     public class Book
     {
      #region Public Properties
    
      public Chapter chapter
      {
       get;
       set;
      }
    
      [XmlAttribute(AttributeName = "Name")]
      public string Name
      {
       get;
       set;
      }
    
      #endregion Public Properties
     }
    
     [Serializable]
     public class Chapter
     {
      #region Public Properties
    
      [XmlAttribute(AttributeName = "number")]
      public int Number
      {
       get;
       set;
      }
    
      public List<Verse> Verse
      {
       get;
       set;
      }
    
      #endregion Public Properties
     }
    
     [Serializable]
     public class Verse
     {
      #region Public Properties
    
      [XmlText]
      public string Value { get; set; }
    
      [XmlAttribute(AttributeName = "number")]
      public int Number
      {
       get;
       set;
      }
    
      #endregion Public Properties
     }
    

     


    Your text file would be like.

     

    EXODUS

     

    1:1: Now these are the names of the children of Israel, which came into Egypt; every man and his household came with Jacob.

    1:2: Reuben, Simeon, Levi, and Judah, 

    1:3: Issachar, Zebulun, and Benjamin,

    1:4: Dan, and Naphtali, Gad, and Asher. 

    1:5: And all the souls that came out of the loins of Jacob were seventy souls: for Joseph was in Egypt already. 

    1:6: And Joseph died, and all his brethren, and all that generation.

    The result xml would be .
     <?xml version="1.0" encoding="utf-8" ?> 
    <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="EXODUS">
    <chapter number="1">
    <Verse>
     <Verse number="1">Now these are the names of the children of Israel, which came into Egypt; every man and his household came with Jacob.</Verse> 
     <Verse number="2">Reuben, Simeon, Levi, and Judah,</Verse> 
     <Verse number="3">Issachar, Zebulun, and Benjamin,</Verse> 
     <Verse number="4">Dan, and Naphtali, Gad, and Asher.</Verse> 
     <Verse number="5">And all the souls that came out of the loins of Jacob were seventy souls</Verse> 
     <Verse number="6">And Joseph died, and all his brethren, and all that generation.</Verse> 
     </Verse>
     </chapter>
     </Book>
    

    There is a additional tag....<Verse> which cannot be avaioded... hope this helps you!!!

     

    i have additional used my code to serialize the object i.e. TOXML() function you can write your own function for serialization.

     


    Regards, Krishnakant This answers, please mark as answered, if this helps please mark as helpful.
    • Marked as answer by ardmore Tuesday, June 14, 2011 12:59 PM
    Tuesday, June 14, 2011 7:38 AM

All replies

  • Hello adrmore,

    From your description, you would like to convert a text file into XML format file. Frankly speaking, this need depends on what format you want. For example, you want to separate your format with chapter number and then you want to filter the verse number, and last is the content. Here’s my option. I think your question isn’t related with what rules you want to create, but more than how to generate XML file. So I suggest you can check XDocument class to generate your XML file. Please check this document for more information about XDocument. http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx

    Hope this helps. If you have any question, please feel free to let me know.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 13, 2011 6:56 AM
  • Hi, Here is the code to read your text file and create xml ... 

    before the code a few assumptions....

    1. First line in the file is always a book name.

    2. 1:1 in this first digit is chapter number, second digit is verse number.

     

     class Program
     {
      #region Private Methods
    
      static void Main(string[] args)
      {
       Book book = new Book();
       List<string> allLines = ReadTextFile(@"C:\Users\kmahamuni\Desktop\Book.txt");
    
       ProcessLines(allLines, out book);
      }
    
      private static void ProcessLines(List<string> allLines, out Book book)
      {
       int lineNumber = 1;
       Book newBook = new Book();
       string[] lineSections = null;
       int currentChapter = 0;
    
       foreach (string line in allLines)
       {
        if (lineNumber == 1)
        {
         newBook.Name = line;
         newBook.chapter = new Chapter();
        }
        else
        {
         lineSections = line.Split(':');
    
         if (lineSections.Length > 1)
         {
          if (currentChapter != Convert.ToInt32(lineSections[0]))
          {
           Chapter chapter = new Chapter();
           currentChapter = Convert.ToInt32(lineSections[0]);
           chapter.Number = currentChapter;
           newBook.chapter = chapter;
           newBook.chapter.Verse = new List<Verse>();
          }
    
          Verse verse = new Verse();
          verse.Number = Convert.ToInt32(lineSections[1]);
          verse.Value = lineSections[2];
          newBook.chapter.Verse.Add(verse);
         }
        }
        lineNumber++;
       }
    
       book = newBook;
    
       string xml = book.ToXML();
      }
    
      private static List<string> ReadTextFile(string fileName)
      {
       if (File.Exists(fileName))
       {
        List<string> lines = new List<string>();
        string line = string.Empty;
    
        using (StreamReader reader = new StreamReader(fileName))
        {
         while ((line = reader.ReadLine()) != null)
         {
          lines.Add(line);
         }
        }
        return lines;
       }
       return null;
      }
    
      #endregion Private Methods
     }
    
     [Serializable]
     [XmlRoot(ElementName = "Book", Namespace = null)]
     public class Book
     {
      #region Public Properties
    
      public Chapter chapter
      {
       get;
       set;
      }
    
      [XmlAttribute(AttributeName = "Name")]
      public string Name
      {
       get;
       set;
      }
    
      #endregion Public Properties
     }
    
     [Serializable]
     public class Chapter
     {
      #region Public Properties
    
      [XmlAttribute(AttributeName = "number")]
      public int Number
      {
       get;
       set;
      }
    
      public List<Verse> Verse
      {
       get;
       set;
      }
    
      #endregion Public Properties
     }
    
     [Serializable]
     public class Verse
     {
      #region Public Properties
    
      [XmlText]
      public string Value { get; set; }
    
      [XmlAttribute(AttributeName = "number")]
      public int Number
      {
       get;
       set;
      }
    
      #endregion Public Properties
     }
    

     


    Your text file would be like.

     

    EXODUS

     

    1:1: Now these are the names of the children of Israel, which came into Egypt; every man and his household came with Jacob.

    1:2: Reuben, Simeon, Levi, and Judah, 

    1:3: Issachar, Zebulun, and Benjamin,

    1:4: Dan, and Naphtali, Gad, and Asher. 

    1:5: And all the souls that came out of the loins of Jacob were seventy souls: for Joseph was in Egypt already. 

    1:6: And Joseph died, and all his brethren, and all that generation.

    The result xml would be .
     <?xml version="1.0" encoding="utf-8" ?> 
    <Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Name="EXODUS">
    <chapter number="1">
    <Verse>
     <Verse number="1">Now these are the names of the children of Israel, which came into Egypt; every man and his household came with Jacob.</Verse> 
     <Verse number="2">Reuben, Simeon, Levi, and Judah,</Verse> 
     <Verse number="3">Issachar, Zebulun, and Benjamin,</Verse> 
     <Verse number="4">Dan, and Naphtali, Gad, and Asher.</Verse> 
     <Verse number="5">And all the souls that came out of the loins of Jacob were seventy souls</Verse> 
     <Verse number="6">And Joseph died, and all his brethren, and all that generation.</Verse> 
     </Verse>
     </chapter>
     </Book>
    

    There is a additional tag....<Verse> which cannot be avaioded... hope this helps you!!!

     

    i have additional used my code to serialize the object i.e. TOXML() function you can write your own function for serialization.

     


    Regards, Krishnakant This answers, please mark as answered, if this helps please mark as helpful.
    • Marked as answer by ardmore Tuesday, June 14, 2011 12:59 PM
    Tuesday, June 14, 2011 7:38 AM
  • If this is something that you only need to do once, why not get the Bible in the xml format already?  It may be easier to adapt your code to read the xml version provided on many sites into your program.  For example, there is a version fairly close to what you asked for at this link: http://www.a-voice.org/bible-vw/download.htm#xml.

    --
    Mike
    Tuesday, June 14, 2011 11:43 AM