combine a few xml files

  • Thread starter Thread starter PZWU
  • Start date Start date
P

PZWU

How can I build a big XML file by appending a few smaller
xml files in C#?

Below is what I've been trying:
/* FileString.xml is the path and name string of the big
xml file*/
XmlTextWriter writer = new XmlTextWriter (FileString,
System.Text.Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteStartElement("FundXML");
/*p_FundOperationFileName is the string name of a smaller
xml file saved under d:\\xml\\FundOperationXML directory */
string Text = AppendXMLFile
(p_FundOperationFileName, "d:\\xml\\FundOperationXML");
writer.WriteString(Text);
writer.WriteEndElement();
writer.Flush();
writer.Close();

in function AppendXMLFile:
string strfile = path +"\\"+filename;
string s = "?>";
try
{
StreamReader sr = new StreamReader(strfile);
String line;
/*while loop removes <?xml version="1.0" encoding="utf-8" ?
from the xml file and return the rest as string append
to the big xml file*/
while ((line = sr.ReadLine()) != null)
{
int idx = line.IndexOf(s);
sr.Close();
return line.Substring(idx+2);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

The big XML constructed this way is not "well formed" if
open it in xmlspy and xslt couldn't retrieve info from it
if it's loaded as xslt source xml file.

Please help and thanks in advance.
 
Do all of the documents have the same schema? If so, you can create a
Dataset and use its ReadXml method. Do it for all three tables. If they
all have the same structure, they should all load. Then use WriteXML to
your given path.

HTH,

Bill
 
Thanks Bill!
The six files combine to form a big file containing full
information about a specific mutual fund. Each smaller
file has different content therefore different schema.
Peter
 
If they have a common column, then check out DataRelations. If you use
these, then they'll be included as well.
 
Bill,
There are no common columns between files. What I'm trying
to do is just creating a xml file with the following form:
<?xml version="1.0" encoding="utf-8?>
<fundxml>
<content of file1 without the xml header>
<content of file2 without the xml header>
<content of file3 without the xml header>
<content of file4 without the xml header>
<content of file5 without the xml header>
<content of file6 without the xml header>
</fundxml>
Here "xml header" refers to <?xml version="1.0"
encoding="utf-8?>
Thanks for your help.
Peter
 
Back
Top