Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hey guys from using below code i tried to convert my text file to xml but i am not succeded with it yet. after lot many tries and efforts i thought to post it on code project. if possible please check my code and provide me related solution for the same.

C#
string statusFilePath = Directory.GetCurrentDirectory().ToString() + "\\Status\\";
            foreach (string file in Directory.GetFiles(statusFilePath, "wpasset*.txt"))
               {
                   string filename = Path.GetFileNameWithoutExtension(file);
                string content = File.ReadAllText(file);
                string statusfilename = Directory.GetCurrentDirectory().ToString() + "\\\\Status\\\\" + "test.xml";

                // string statusFilePath1 = Directory.GetCurrentDirectory().ToString() + "\\\\Status\\\\" + "*.xml";
                Regex expression = new Regex(@"##(?<parent>\w+)\r\n(?<children>[\w ]+)\r\n((?<values>[\S ]+)(\r\n(?!#)|$))+", RegexOptions.None);
                XElement xeRoot = new XElement("wpassetd");   //root node
                foreach (Match m in expression.Matches(content))
                {
                    XElement xeParent = new XElement(m.Groups["parent"].Value);

                    string[] children = m.Groups["children"].Value.Split(' ');      // Use space as delimter for the children

                    foreach (Capture cap in m.Groups["values"].Captures)
                    {
                        XElement xeChild = new XElement("data");   // Change the name 'child' to whatever suitable
                        string[] values = cap.Value.Split(new string[] { ":" }, StringSplitOptions.None);
                        // Check that the children and values counts are equal
                        if (children.Length != values.Length)
                            throw new Exception("The number of children and values mismatch.");

                        for (int i = 0; i < children.Length; i++)
                        {
                            XElement xeChildValue = new XElement(children[i].ToLower());

                            //xeChildValue.Value = values[i];

                            //xeChildValue.Value = String.Format("![CDATA[{0}]]", values[i]);
                            XCData cdata = new XCData(values[i]);
                            xeChildValue.Add(cdata);

                            xeChild.Add(xeChildValue);

                        }
                        xeParent.Add(xeChild);

                    }

                    xeRoot.Add(xeParent);
                }
                XDocument xmldoc = new XDocument();
                string result;
                xmldoc.Add(xeRoot);
                xmldoc.Save(statusfilename);
                result = Path.GetFileName(statusfilename);
                //xmldoc.Save(@"test.xml");    


and my text file is :
2015-04-07 14:49:33:(INFO) : Category: HandleOutPut: Starting log 2015-04-07 14:49:33
2015-04-07 14:49:33:(WARN) : Category: IsRunAsAdmin: The user running this process is not an elevated administrator. Some commands will not work properly without it. Please make sure that the process has local administrator privileges and "Run as administrator"(UAC). 
2015-04-07 14:49:33:(INFO) : Category: -Unknown Operation-: 
2015-04-07 14:49:37:(ERROR) : Category: error: Failed while calling WS-Management call 
2015-04-07 14:49:37:(ERROR) : Category:The caller is unauthorized. 
2015-04-07 14:49:37:(WARN) : Category: -Initializing connection-: Failed connecting locally , will try to connect using the remote server. 
2015-04-07 14:49:41:(ERROR) : Category: LoadXMLFromFile: The profile does not contain the mandatory tag. 
2015-04-07 14:49:41:(ERROR) : Category: Exit: ***********Exit with code 38. Details: Error with XML file (possible reasons - the file does not exist or access to it is denied; the file contains incorrect parameters; incorrect or missing encryption password/parameter; invalid data)    


output should be in following format:
XML
<myfile>
<errorcode>38</errorcode>
<error>Failed while calling WS-Management call.-Initializing connection-:Failed connecting locally , will try to connect using the remote server. LoadXMLFromFile: The XML does not contain the mandatory tag.Error with XML file (possible reasons - the file does not exist or access to it is denied; the file contains incorrect parameters; incorrect or missing encryption password/parameter; invalid data) </error>
</myfile>
Posted
Updated 23-Apr-15 21:02pm
v2
Comments
Kats2512 24-Apr-15 2:33am    
are you running visual studio as an administrator?
nishac189 24-Apr-15 8:04am    
yes
Sinisa Hajnal 24-Apr-15 2:41am    
Same as the last time, you need to check access rights to the file.
Maciej Los 24-Apr-15 3:05am    
What have you done till now? Where are you stuck?
nishac189 24-Apr-15 7:30am    
I want only error line in tag like "Failed while calling WS-Management call"
but can u please give me a trick how to trim that ERROR line.?

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900