write into TEXT file using C#
Using C# i need to
Read the XML file, data by data and write these datas into the Text file( like - date.txt)
MSH|^~\$|ADMIT|CLAY COUNTY MEMORIAL|||200502110938||ADT^A01|00860|P|2.2||||||
MSH|^~\$|ADMIT|CLAY COUNTY MEMORIAL|||200502110938||ADT^A01|00860|P|2.2||||||
I guess you could maybe load it into a dataset, since looking at the top of the Xml file seems to have saved it from a dataset?
then pretty much loop through each row/column and write to the textfile using the StreamWriter.
here is a quick pseudo:
foreach row in theDataSetTable
foreach item in row.ItemArray
writeToFile(item)
end foreach
end foreach
XmlTextReader tr = new XmlTextReader("c:/xml/XMLFile3.xml");
Iam able to read the xml and not able to write it in the text file.
how did you save the xml file? From a dataset? (theDataSet.WriteXml()) - if so ...
try this:
DataSet theDataSet = new DataSet(); theDataSet.ReadXml(pathToXmlFile); //loads xml into dataset StreamWriter theWriter = new StreamWriter(Application.StartupPath + "\\test.txt"); foreach (DataRow curRow in theDataSet.Tables[0].Rows) { foreach (object curObjectValue in curRow.ItemArray) { theWriter.Write(curObjectValue); } } theWriter.Close(); |
MSH|^~\$|ADMIT|CLAY COUNTY MEMORIAL|||200502110938||ADT^A01|00860|P|2.2|||||| How can i pass these delimiters to the below text file ?. |^~\$ADMIT200502110938ADT^A0100860P2.2
do you mean after writing everything to the textfile?
if this is the case, then just before we close the StreamWriter (theWriter), we just write that:
theWriter.Write("|^~\$ADMIT200502110938ADT^A0100860P2.2");
theWriter.Close();
does this help?
I have an XML file - XMLFile3.xml Now iam getting the text value like this |^~\$ADMIT200502110938ADT^A0100860P2.2 Now i want to pass this for example:
Public Sub XML_to_CSV(ByVal strFileName As String, ByVal strHeader As String, ByVal strOutput As String)
' Open an XML file
Dim reader As XmlTextReader = New XmlTextReader(strFileName)
' This needs moving from this location to somewhere cleaner
Dim doc As New XmlDocument() 'Define a new XmlDocument
doc.Load(reader) ' Read in our XML file
doc.PreserveWhitespace = True 'Make sure that we save all the white space (empty fields)
Dim strRecord As String = "" ' Varible that we will store the record in.
Dim objStreamReader As StreamReader
'open the file
objStreamReader = File.OpenText(strHeader)
'Read the file storing it in the readfile string
Dim readfile As String = objStreamReader.ReadToEnd()
objStreamReader.Close()
Dim fs As New FileStream(strOutput, FileMode.Create, FileAccess.Write)
strRecord = readfile
Dim s As New StreamWriter(fs)
s.WriteLine(strRecord)
strRecord = ""
For Each clsRootNode As System.Xml.XmlNode In doc.ChildNodes
If clsRootNode.NodeType = Xml.XmlNodeType.Element Then
For Each clsChildNode As System.Xml.XmlNode In clsRootNode.ChildNodes
'MessageBox.Show(clsChildNode.Name & ":" & clsChildNode.InnerText)
If clsChildNode.Name = "MSH" Then 'If we find a row
For Each clsItemNode As System.Xml.XmlNode In clsChildNode.ChildNodes
' Process each of the records...
If clsItemNode.Name.Contains("date") Or clsItemNode.Name.Contains("Date") Then
strRecord += Date.Parse(clsItemNode.InnerText).ToShortDateString + "|" 'Add a collumn
Else
strRecord += clsItemNode.InnerText + "|" 'Add a collumn
End If
Next 'Next child
strRecord = strRecord.Substring(0, strRecord.Length - 1) 'Kill of the final comma
s.WriteLine(strRecord) 'Write the line to the file
strRecord = "" ' We have written to the file so kill it off
End If
Next
End If
Next
reader.Close()
s.Close()
End Sub
On the line ( strRecord += clsItemNode.InnerText + "|" 'Add a collumn ) is where your field divider is added. This is normally where the CSV comma would be but you can put what you like. It will need fitting to the XML schema you have but it should do what you want.