开发者

Creating large xml using XMLWriter in C#

开发者 https://www.devze.com 2023-01-23 10:59 出处:网络
I was trying to create an xml which contains a large data using XMLWriter, the function has executed with out any exception.But when I opened the XML, I found that the created XML was not complete, it

I was trying to create an xml which contains a large data using XMLWriter, the function has executed with out any exception.But when I opened the XML, I found that the created XML was not complete, it was broken in mid way.I dont know what I am doing wrong .Is there any default size limit for XMLWriter? Is XMLWriter the best way to create large XMLs? if not please let me know what is the best way to create large xml? Does using XDocument make my life easier?

this is my code structure (I cannot put my orginal code here !! :( )

using (//file stream)
{
开发者_StackOverflow社区     XmlWriter mywriter = new XmlWriter.Create(@"C:\mydata.xml");
       // write start element1
       // write start element2

                     while (//not end of file)
                    {
                    switch (entrytype)
                    {
                        Case 1:
                           // create elements
                        Case 2:
                           // create elements
                        so on ....
                    }
                    }

       // write end element2
       // write endelement1
}

The size of the XML is expected to be a few hundreds KB.

Please reply..


I suspect you simply didn't close the write properly:

using (//file stream)
{
     using(XmlWriter mywriter = new XmlWriter.Create(@"C:\Auditlog.xml")) {
       // write start element1
       // write start element2

                     while (//not end of file)
                    {
                    switch (entrytype)
                    {
                        Case 1:
                           // create elements
                        Case 2:
                           // create elements
                        so on ....
                    }
                    }

       // write end element2
       // write endelement1
    }
}

Note the extra using - without that the writer may have some data still buffered. This would generally account for a missing end to the data; we obvbiously can't diagnose it exiting midway without a reproducible example.

Note also: xml will work for large files, but it isn't necessarily the best choce - a few hundred k should be fine though.


XmlWriter is exactly the tool for creating large XML files and the size limit will be dictated by the underlying stream which in your case is the file system so as long as you have free space on this it should be OK.

Every time you write a start element you should make sure to provide the corresponding end element or an exception will be thrown. You haven't provided a full example of your code illustrating the problem so cannot say for sure what's wrong.

Also you don't need to use a file stream when you specify a filename in the constructor:

using (var writer = new XmlWriter.Create(@"C:\mydata.xml"))
{
    // TODO: use the writer here    
}


using (//file stream)
{
     using(XmlWriter mywriter = new XmlWriter.Create(@"C:\Auditlog.xml")) {
       // write start element1
       // write start element2

                     while (//not end of file)
                    {
                    switch (entrytype)
                    {
                        Case 1:
                           // create elements
                        Case 2:
                           // create elements
                        so on ....
                    }
                    }

       // write end element2
       // write endelement1

       mywriter.Close();

    }
}

Few hundreds KB is not that Big! Don't Worry! :)

Regards,

Mazhar Karimi

0

精彩评论

暂无评论...
验证码 换一张
取 消