|
I do ensure all nodes are closed when the end of file is found - at least I think so, but I am sure it is something in that area that is causing the problem.
As for the exception, there isn't one; the program doesn't complete. The XML file's are created, but in the last one towards the end of the file, in the middle of a record is the message:
"XML page cannot be displayed
The following tags were not closed: message."
Where message is my root node. Yet it closes them in the other files (when the original XML file still has more records to read)...
|
|
|
|
|
Can you show the bits of code which are executed when the end-of file is reached?
Just Google it.
Failing that try phoning
|
|
|
|
|
Here is my code:
while (XmlReader.Read() && nodeCounter <= 1000)
{
// Create a file to write to
XmlTextWriter XmlWriter = new XmlTextWriter(directory + "\\Output" + x + " CIS_KASN.xml", null);
XmlWriter.Formatting = Formatting.Indented;
// Write the header details to the new file
XmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'");
XmlWriter.WriteComment("This XML message contains CIS Key Accounts and Special Needs records for inserting/updating in the IAR");
XmlWriter.WriteStartElement("message");
XmlWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
XmlWriter.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "CISKASN.xsd");
// Write each row to the file, until 1000 records have been written
for (int i = 1; i <= 1000; i++)
{
while (XmlReader.Name != "CISKASNRecord")
{
XmlReader.Read();
}
XmlWriter.WriteNode(XmlReader, true);
XmlWriter.Flush();
}
// Tidy up and close the file so that a new one can be opened
XmlWriter.Flush();
XmlWriter.Close();
nodeCounter++;
x++;
}
The program runs fine if I reduce my for-loop to 10 (thus not reading the end of file). At 1000 I get an XML error saying the 'message' tag is not closed and the program doesn't complete...
|
|
|
|
|
bigove wrote: for (int i = 1; i <= 1000; i++)
You need a condition here to test for the EOF me thinks. You're loop statements look a bit dodgy, might I suggest something like this:
private void Process(XmlReader xr)
{
int recordCount = 0;
int fileCount = 0;
XmlTextWriter xtw = OpenFile(fileCount++);
while (xr.Read())
{
<font face="Courier New"> </font><font face="Courier New">while xr != "CISKASNRecord")
xr.Read();
xtw.WriteNode(xr, true);</font>
<font face="Courier New"> xtw.Flush();</font>
recordCount++;
if (recordCount == 1000)
{
xtw.Close();
xtw.Flush();
xtw = null;
xtw = OpenFile(fileCount++);
}
}
if (xtw != null)
{
xtw.Close();
xtw.Dispose();
xtw = null;
}
}
private XmlTextWriter OpenFile(int index)
{
XmlTextWriter xtw = new XmlTextWriter(Path.Combine(directory, string.Format("Output{0}CIS_KASN.xml", index)), null);
xtw.Formatting = Formatting.Indented;
<font face="Courier New"> xtw.WriteProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'");
xtw.WriteComment("This XML message contains CIS Key Accounts and Special Needs records for inserting/updating in the IAR");
xtw.WriteStartElement("message");
xtw.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
xtw.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "CISKASN.xsd");
return xtw;</font>
} The benefit of this solution is there is only one place where you check for the end-of-file condition and similarly one place where you check for the end-of-batch condition.
Just Google it.
Failing that try phoning
|
|
|
|
|
Thank you for the help.
Trying that code, the 'Dispose' method is inaccessible. But commetning that out, the code compiles...but nothing is output. Am I missing something obvious?!
|
|
|
|
|
bigove wrote: 'Dispose' method is inaccessible
Oops, my memory must be getting full
bigove wrote: Am I missing something obvious?!
No I probably am.
Give me a few minutes to check over the code.
Just Google it.
Failing that try phoning
|
|
|
|
|
The condition within the second while loops is incorrect, should be while (xr.Name != "...") . It's never ending so it scans through the whole file thus nothign is output (I think).
Just Google it.
Failing that try phoning
|
|
|
|
|
I'd already noticed that and added the 'Name' part to my code - but still do not get any output...
|
|
|
|
|
Have you tried stepping through the code in the debugger, or just write something to the console just so you can see what order things are being done in.
Just Google it.
Failing that try phoning
|
|
|
|
|
I have, things work in the right order. It looks to be a problem reading the end of the file, it's always the last file output that fails...
|
|
|
|
|
Did you notice anything different in it's operation when you were getting to the the end of the file? (While debugging).
Just Google it.
Failing that try phoning
|
|
|
|
|
No. It did seem to go through the for-loop a few too may times in a weird way, but still worked 'as expected' (at least until the end of file was reached). Which I didn't understand, I have to admit.
Trying a different plan, I have tried a switch statement rather than a for-loop; this seems to reach the end of the file OK, but has weird output. It creates 1000+ files of one record each, but doesn't begin writing nodes (only the header info) until record 23! The switch statement is (in place of the for-loop):
switch (XmlReader.Name)
{
case "CISKASNRecord":
XmlWriter.WriteNode(XmlReader, true);
break;
case "message":
XmlWriter.WriteFullEndElement();
break;
}
Does this make more sense?
|
|
|
|
|
Just noticed that in the snippets I provided I did not reset recordCount to 0 inside the if (recordCount == 0) . Have you done this?
Just Google it.
Failing that try phoning
|
|
|
|
|
THere is nor if (recordCount == 0); there is one that checks to see if it was 1000, but surely that should be the case?
|
|
|
|
|
Sorry, inside the check for 1000 records (where we close the file and open a new one) it should also set recordCount to 0 otherwise more files will not be created.
|
|
|
|
|
Still no joy - no files are output. The program runs and completes, but there are no files created.
|
|
|
|
|
Any chance of sending the Xml file?
|
|
|
|
|
Not really, as it's a work thing and I'd be fired if it went outside!
But the format is EXACTLY as follows:
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <!--
This XML message contains CIS Key Accounts and Special Needs records for
inserting/updating in the IAR
-->
- <message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CISKASN.xsd">
- <messageHeader>
<batchID>1</batchID>
<records>1016</records>
<messageNo>20060918165555</messageNo>
</messageHeader>
- <CISKASNRecord>
<changeFlag>I</changeFlag>
<propertyNum>123456789</propertyNum>
<pointX>533935.3</pointX>
<pointY>189061.2</pointY>
<osAPR>AP70H9815GH56N2GHR</osAPR>
<keyActCode>F</keyActCode>
<dialysisCust>Y</dialysisCust>
<serviceCode>21</serviceCode>
<circumstanceCode>1</circumstanceCode>
<sicCode>999</sicCode>
<hospitalCode>CODEHERE</hospitalCode>
<hospitalName>Hospital Name</hospitalName>
<address1>Address Line 1</address1>
<address2>Address Line 2</address2>
<address3>Address Line 3</address3>
<address4>Town/City</address4>
<address5>County</address5>
<postcode>Postcode</postcode>
</CISKASNRecord>
- <CISKASNRecord>
Same again - for however many records
</CISKASNRecord>
</message>
|
|
|
|
|
Ok, thanks for that though, I'm afraid I can't do much for the next couple of hours as I'm going out to help someone, but after that I'll take a look.
|
|
|
|
|
I've got it working! Using my code as I'd spent so long on it!
Basically, it was cycling through at the end of the file trying to read a blank line every time, rather than the closing tag. I'd tried an if statement to break out of the loop, but that never broke out the next one; so adding another break statement cured thr problem. At last!
The only problem is adding that 'messageHeader' node - I can't seem to manage that! It should be simple-ish though, so I will persevere...
Thank you very much for your help, much appreciated.
Cheers again.
|
|
|
|
|
hi
i just want to ask how to write javascripts in a content page...
i mean, sould i put the script like this:
<asp:content>
<script>
......
</script>
....
</asp:content>
Tamimi - Code
|
|
|
|
|
hi,
if i press a button in a menu in a parent form i open a child form called form2, if i choose form3 in the menu i want to close form2 and show form3, and visa versa, but i cant do this.
i get an error because the form2 is closed and i cant reopen it.
it has someting to doe with the signle instance... i dont understand it
this i what i have of code, very simple it just opens empty forms:
namespace swapform
{
public partial class Form1 : Form
{
Form2 objchild1 = new Form2();
Form3 objchild = new Form3();
public Form1()
{
InitializeComponent();
objchild1.MdiParent = this;
objchild.MdiParent = this;
}
private void form3ToolStripMenuItem_Click(object sender, EventArgs e)
{
objchild.Show(); //show form3
objchild1.Close(); // close form 2
}
private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
objchild1.Show(); //show form 2
objchild.Close();//close form 3
}
}
}
|
|
|
|
|
Use the Hide method to hide the window. Once you close it you can't show it any more.
---
b { font-weight: normal; }
|
|
|
|
|
i found a way, cause when i just hide the form is still open and i dont want that.
here is the solution to be able to close a child form when opening another.
namespace swapform
{
public partial class Form1 : Form
{
Form3 objchild = new Form3();
Form2 objchild1 = new Form2();
public Form1()
{
InitializeComponent();
objchild.MdiParent = this;
objchild1.MdiParent = this;
}
private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
objchild1.Close();
if ((objchild == null) | (objchild.IsDisposed))
{
objchild = new Form3();
objchild.MdiParent = this;
}
objchild.Show(); // show form3
objchild.Top = 10;//specify the location
objchild.Left = 10;
objchild.Height = 550;
objchild.Width =560;
}
private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
{
objchild.Close();
if ((objchild1 == null) | (objchild1.IsDisposed))
{
objchild1 = new Form2();
objchild1.MdiParent = this;
}
objchild1.Show(); // show form 2
objchild1.Top = 10;//specify the location
objchild1.Left = 10;
objchild1.Height = 550;
objchild1.Width = 560;
}
}
grz
|
|
|
|
|
Why do you create the child forms in the constructor at all? Why don't just create them when you need them? Then you don't need the extra code to handle the case when they exist already.
---
b { font-weight: normal; }
|
|
|
|