Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3344524
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:04:46+00:00 2026-05-18T01:04:46+00:00

I am working with a large xml file containing unbounded collections. The idea is

  • 0

I am working with a large xml file containing unbounded collections. The idea is to use XmlReader to read the file and to deserialize the inner xml into an object and do further processing.

The XML structure is something like this:

<Basket xmlns="http://AppleFarm.com/Basket">
 <AppleCount>10000</AppleCount>
 <Apples>
  <Apple><ID>1</ID><Color>Red</Color></Apple>
  <Apple><ID>2</ID><Color>Green</Color></Apple>
  ...
  <Apple><ID>10000</ID><Color>Green</Color></Apple>
 </Apples>
</Basket>

Everything goes well using XMLReader wrapping around XMLTextReader to read the file. However when I tried to deserialize individual apple into an object it throws InvaildOperationException.

Anyone knows whats the problem? Is there a better way to do it?

Here are the code fragments

//Deserialize code
public object Deserialize(XmlDocument doc, Type type){
    using(XmlNodeReader reader - new XmlNodeReader(doc.DocumentElement)){
        XmlReaderSetting settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.None;
        using(XmlReader xReader = XmlReader.Create(reader, settings)){
            XmlSerializer serializer = new XmlSerializer(type);
            object obj = serializer.Deserialize(xReader);
        }
    }
}

public void GetApples(string filepath){
    XmlTextReader reader = new XmlTextReader(filepath);
    while(reader.Read()){
        while(reader.NoteType == XmlNodeType.Element &&
            reader.Name == "Apple"){
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reader.ReadOuterXml());
            Apple a = (Apple)Deserialize(doc, typeof(Apple));

            //...
        }
    }
}
//Deserialize code end

//Apple class
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.XmlSerialization.XmlTypeAttribute(Namespace="http://AppleFarm.com/Basket")]
public partial class Apple{
    private string idField;
    private string colorField;

    public string Id{
        get{ return this.idField; }
        set{ this.idField = value; }
    }

    public string Color{
        get { return this.colorField; }
        set { this.colorField = value; }
    }
}
//Apple class end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:04:47+00:00Added an answer on May 18, 2026 at 1:04 am

    XML deserializing need first line to be

    <?xml version="1.0" encoding="utf-8"?>
    

    If you want to convert a part of xml document to Object you will have to append this line at top of the partial XML.

    Plus you need to decorate Apple class with XmlRootAttribute where ElementName will be ‘Apple’

    this article describe how to set XmlRootAtrribute
    http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute.aspx

    Hope this will help

    Reagards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some code to convert a large (many gigabytes) XML file into another
I have a large xml file (40 Gb) that I need to split into
I am trying to split up a large xml file into smaller chunks. I
Currently I'm working on an application that has to parse a large XML file
I am currently working with a large XML file which includes <Count>123</Count> for some
I ran into some problems using jQuery to read an external xml file and
I'm working on a project involving some large XML files (from 50MB to over
I'm relatively new to working with XML and am working with some rather large
I often jump from a file to file, while working a large codebase. I
I have a large xml file (1Gb). I need to make many queries on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.