Click here to Skip to main content
15,921,250 members
Home / Discussions / C#
   

C#

 
QuestionHow do you read the end of an XML file in C#? Pin
bigove24-Sep-06 22:55
bigove24-Sep-06 22:55 
AnswerRe: How do you read the end of an XML file in C#? Pin
Ed.Poore24-Sep-06 23:04
Ed.Poore24-Sep-06 23:04 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove24-Sep-06 23:52
bigove24-Sep-06 23:52 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore25-Sep-06 0:36
Ed.Poore25-Sep-06 0:36 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove25-Sep-06 1:58
bigove25-Sep-06 1:58 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore25-Sep-06 2:08
Ed.Poore25-Sep-06 2:08 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove25-Sep-06 2:55
bigove25-Sep-06 2:55 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore25-Sep-06 4:54
Ed.Poore25-Sep-06 4:54 
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;                        // Counter to count how many records are in current file.
    int fileCount = 0;                          // Counter for file index
    XmlTextWriter xtw = OpenFile(fileCount++);  // XmlTextWriter for output file
    // Loop until EOF
    while (xr.Read())
    {
         // Skip over records which are not to be output
<font face="Courier New">         </font><font face="Courier New">while xr != "CISKASNRecord")
            xr.Read();
         // Write the element to output
        xtw.WriteNode(xr, true);</font>
<font face="Courier New">        xtw.Flush();</font>
        // Increment node counter
         recordCount++;

         // Check to see if 1000 records have been written
         if (recordCount == 1000)
         {
                // Close the output stream
                xtw.Close();
                xtw.Flush();
		xtw = null;		// Set to null to check to see if stream is open at the end of the loops
		// Open a new stream
		xtw = OpenFile(fileCount++);
         }
    }
    // Close the stream if not done already
    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;
    // Write header details
   <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 the object
    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 Green Alien | [Alien]

GeneralRe: How do you read the end of an XML file in C#? Pin
bigove25-Sep-06 5:24
bigove25-Sep-06 5:24 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore25-Sep-06 6:13
Ed.Poore25-Sep-06 6:13 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore25-Sep-06 6:15
Ed.Poore25-Sep-06 6:15 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove25-Sep-06 22:13
bigove25-Sep-06 22:13 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore25-Sep-06 23:28
Ed.Poore25-Sep-06 23:28 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove25-Sep-06 23:44
bigove25-Sep-06 23:44 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore26-Sep-06 0:03
Ed.Poore26-Sep-06 0:03 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove26-Sep-06 0:20
bigove26-Sep-06 0:20 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore26-Sep-06 0:25
Ed.Poore26-Sep-06 0:25 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove26-Sep-06 0:36
bigove26-Sep-06 0:36 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore26-Sep-06 1:05
Ed.Poore26-Sep-06 1:05 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove26-Sep-06 1:25
bigove26-Sep-06 1:25 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore26-Sep-06 1:30
Ed.Poore26-Sep-06 1:30 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove26-Sep-06 1:35
bigove26-Sep-06 1:35 
GeneralRe: How do you read the end of an XML file in C#? Pin
Ed.Poore26-Sep-06 1:49
Ed.Poore26-Sep-06 1:49 
GeneralRe: How do you read the end of an XML file in C#? Pin
bigove26-Sep-06 2:53
bigove26-Sep-06 2:53 
QuestionContent page script asp.net 2005 Pin
Tamimi - Code24-Sep-06 22:45
Tamimi - Code24-Sep-06 22:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.