I have an XML question si anyone is up for it.
I have small groups of xml documents (typically representing 2 or three pages of data - 1 xml doc = 1 page), the application that I'm writing needs to concatinate these into one xml document.
so I have the following code:
string
xmlFilename = "c:\\temp\\pauls.xml";XmlTextWriter writer = new XmlTextWriter(xmlFilename, System.Text.Encoding.UTF8);
writer.Formatting = System.Xml.Formatting.Indented;
writer.WriteStartDocument();
writer.WriteComment("This is a sample XML document");
writer.WriteStartElement("Document");
foreach (object obj in super_object)
{
XmlTextReader xr = new XmlTextReader(fileToLoad);
xr.ReadStartElement("result");
good god I'm lost
writer store data
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
now, strangely enough at the point labeled 'good god I'm lost' - I'm lost.
I've tried doing many many different things here, some would compile some wouldn't, none produced the result I was after.
can anyone fill in the blank for me, these are not complex schemas that I'm running from, all start with the root node 'result' followed by an attribute.
HELP!!!!
thanks,
Ed.

XML Parsing Question
Nicolas Humann
my you give us an example for your xml-files
Toop
an example is as follows:
< xml version="1.0" encoding="ISO-8859-1" >
<result page="35.0.8.0">
<OSDATA name="Nom" value=""/>
<OSDATA name="Date" value=""/>
</result>
see, very simple and the thing I'd like to implement is
<doc>
<page1>
<fields>
</page1>
<page2>
blah
blah
</doc>
thanks in advance,
Ed.
Gidion
So assume you have these two xml files:
doc1.xml
<doc1>
<node1>abc</node1>
</doc1>
doc2.xml
<doc2>
<node1>def</node1>
</doc2>
You can use the following transform (or similar) to put them together:
<
xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="document('doc1.xml')/doc1/node1" />
<xsl:copy-of select="document('doc2.xml')/doc2/node1" />
</xsl:template>
</xsl:stylesheet>
You'll have to work with the nodes and the template, but this should get you going, at least if you want to go this direction.
Let me know if you have any more questions about this.
Alex Thaman
thanks for the reply.
Is there any way of doing this dynamically in C# rather than using a specific xsml
thanks again,
Ed.
Mr-Asrawi
yes, when I went this route with my code, this is what i did:
i declared two string constants acting as templates:
(I apologize ahead of time for the formatting)
private const string TRANSFORM_TEMPLATE = "<xsl:stylesheet version="1.0" xmlns:xsl="//www.w3.org/1999/XSL/Transform" href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
{0}
</xsl:template>
</xsl:stylesheet>";
private const string DOCUMENT_TEMPLATE = "<xsl:copy-of select="document('{0}')/doc1/node1" />"
so, lets assume you have 3 documents to combine, and the paths are stored in a string array:
private void LoadData()
{
XslTransform transform = new XslTransform();
string[] files = {"doc1.xml", "doc2.xml", "doc3.xml"};
string documents = "";
for(int i=0; i<files.Length; i++)
{
documents += string.Format(DOCUMENT_TEMPLATE, files[ i ]);
}
transform.Load(string.Format(TRANSFORM_TEMPLATE, documents));
}
The above snippet of code is close to being just pseudo code. I did not proof the syntax with the IDE, but is just there to show how you can dynamically create the text for your xslt.
JRafe
From recollection, what I noticed was that when I called transform.Load, it took x amount of time to call it, probably because of the transform pulling in each document. Now, just because you have the transform loaded, doesnt mean you have the document. Now, you actually have to apply the transform using the transform.Transform function. Again, this call took x amount of time to perform.
What I ended up doing, and what I found that loading each document one at time and importing a parent node from it was much faster. I didn't realize the XmlDocument.ImportNode function was so fast! I basically ended up doing the following:
string[] files = {"doc1.xml", "doc2.xml", "doc3.xml"};
XmlDocument document, mainDocument;
XmlNode mainNode, newNode;
document = new XmlDocument();
mainDocument = new XmlDocument();
mainNode = mainDocument.AppendChild(parentNode.CreateElement("mainnode"));
for (int i=0; i<files.Length; i++)
{
document.Load(files[ i ]);
newNode = document.SelectSingleNode("parentnode");
mainNode.AppendChild(mainDocument.ImportNode(newNode, true));
}
I wrote this snippet with minimal proofing, and was broken out so that you can see what's going on with each node.
I currently don't have the timings present, but I did see a performance improvement doing it this, not to mention is relatively simpler than my first method.