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

  • SEARCH
  • Home
  • 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 8900469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:06:20+00:00 2026-06-15T01:06:20+00:00

I’m using XmlSerializer to serialize and then deserialize a simple object. When I deserialize

  • 0

I’m using XmlSerializer to serialize and then deserialize a simple object. When I deserialize the object to my surprise I find a child object was not properly deserialized but instead turned into XmlNode[].

Here is very nearly the structure I’ve got:

// This line I put in here as a way of sneaking into the XML the 
// root node's C# namespace, since it's not the same as the 
// deserializing code and the deserializing code seemed unable to 
// deserialize properly without knowing the Type (see my code below).
// So I basically just use this fake construct to get the namespace 
// and make a Type of it to feed the XmlSerializer() instantiation.
[XmlRoot(Namespace = "http://foo.com/CSharpNamespace/Foo.Bar")]   

// This is because QueuedFile can be given to the Argument array.
[XmlInclude(typeof(QueuedFile))]
// This class is Foo.Bar.CommandAndArguments
public class CommandAndArguments {
    public String Command;
    public object[] Arguments;
}

// I don't think this matters to XmlSerialize, but just in case...
[Serializable()] 

// I added this line just thinking maybe it would help, but it doesn't
// do anything.  I tried it without the XmlType first, and that
// didn't work.
[XmlType("Foo.Baz.Bat.QueuedFile")]

// This class is Foo.Baz.Bat.QueuedFile (in a different c# 
// namespace than CommandAndArguments and the deserializing code)
public QueuedFile {
    public String FileName;
    public String DirectoryName;
}

And the code which deserializes it looks like:

public static object DeserializeXml(String objectToDeserialize)
        {
            String rootNodeName = "";
            String rootNodeNamespace = "";

            using (XmlReader xmlReader = XmlReader.Create(new StringReader(objectToDeserialize)))
            {
                if (xmlReader.MoveToContent() == XmlNodeType.Element)
                {
                    rootNodeName = xmlReader.Name;
                    rootNodeNamespace = xmlReader.NamespaceURI;

                    if (rootNodeNamespace.StartsWith("http://foo.com/CSharpNamespace/"))
                    {
                        rootNodeName = rootNodeNamespace.Substring("http://foo.com/CSharpNamespace/".Length) + "." +
                                       rootNodeName;
                    }
                }
            }

            //MessageBox.Show(rootNodeName);
            try
            {
                Type t = DetermineTypeFromName(rootNodeName);

                if (t == null)
                {
                    throw new Exception("Could not determine type of serialized string.  Type listed as: "+rootNodeName);                    
                }

                var s = new XmlSerializer(t);
                return s.Deserialize(new StringReader(objectToDeserialize));

                // object o = new object();
                // MethodInfo castMethod = o.GetType().GetMethod("Cast").MakeGenericMethod(t);
                // return castMethod.Invoke(null, new object[] { s.Deserialize(new StringReader(objectToDeserialize)) });
            }
            catch (InvalidOperationException)
            {
                return null;
            }
        }

And here is the XML when the CommandAndArguments is serialized:

<?xml version="1.0" encoding="utf-16"?>
<CommandAndArguments xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://foo.com/CSharpNamespace/Foo.Bar">
  <Command>I am a command</Command>
  <Arguments>
    <anyType xsi:type="Foo.Baz.Bat.QueuedFile">
      <FileName xmlns="">HelloWorld.txt</FileName>
      <DirectoryName xmlns="">C:\foo\bar</DirectoryName>
    </anyType>
  </Arguments>
</CommandAndArguments>

But when I deserialize I am given a CommandAndArguments object where Arguments is XmlNode[] with the first item being the attribute giving the QueuedFile as the type and the other indices being elements of the properties. But why wasn’t the QueuedFile object recreated?

I suspect this might somehow have do with C# namespaces and the engine doing the deserializing not being able to find or work with QueuedFile… But I don’t see why since when I forgot the XmlInclude() it made sure to tell me it didn’t expect QueuedFile and now that I’ve added the XmlInclude() I get no error, just an incomplete deserialization.

Help? I’ve read everything I can find to read and Googled everything I know to Google and am stuck. I certainly have a lot to learn about XML serialization but I’m not sure how I’m failing at something which should be pretty simple (I actually did something almost exactly like this before without any problem, the only difference then was that everything was in the same C# namespace).

  • 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-06-15T01:06:21+00:00Added an answer on June 15, 2026 at 1:06 am

    Are you able to change the XML format or is it fixed? I don’t know what the problem you are having is, but I use the DataContractSerializer classes extensively with no problems.

    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

    public static void WriteObject(string fileName)
            {
                Console.WriteLine(
                    "Creating a Person object and serializing it.");
                Person p1 = new Person("Zighetti", "Barbara", 101);
                FileStream writer = new FileStream(fileName, FileMode.Create);
                DataContractSerializer ser =
                    new DataContractSerializer(typeof(Person));
                ser.WriteObject(writer, p1);
                writer.Close();
            }
    
            public static void ReadObject(string fileName)
            {
                Console.WriteLine("Deserializing an instance of the object.");
                FileStream fs = new FileStream(fileName,
                FileMode.Open);
                XmlDictionaryReader reader =
                    XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
                DataContractSerializer ser = new DataContractSerializer(typeof(Person));
    
                // Deserialize the data and read it from the instance.
                Person deserializedPerson =
                    (Person)ser.ReadObject(reader, true);
                reader.Close();
                fs.Close();
                Console.WriteLine(String.Format("{0} {1}, ID: {2}",
                deserializedPerson.FirstName, deserializedPerson.LastName,
                deserializedPerson.ID));
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.