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 7998827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:19:39+00:00 2026-06-04T15:19:39+00:00

I have some xml that is provided with generic attributes (name, type, etc), where

  • 0

I have some xml that is provided with generic attributes (name, type, etc), where the value of the attribute needs to be the property into which the xml is deserialized.

A sample of the type of xml I’m trying to push onto a class would be:

<root>
    <company>
        <location>USA</location>
        <name>TopCars</name>
    </company>
    <CarList name="CarCounts">
        <ModelList name="Models">
            <Column name="Ford">50</Column>
            <Column name="Chevy">65</Column>
            <Column name="Dodge">75</Column>
        </ModelList>
    </CarList>
</root>

I can provide some sample code I’ve used, but it doesn’t work and I’d like some fresh outlooks. I was using an xmlReader deserialized by a class that had a single property identified by the attribute “name” which isn’t what I want anyhow. I ultimately want like Class Models which contains Ford, Chevy, and Dodge properties (this is a made up example if you object to the car structure here).

XSD spits out something like this which is kind of what I would expect, but not really what I want:

[...]
<xs:element name="Column" nillable="true" minOccurs="0" maxOccurs="unbounded">
  <xs:complexType>
    <xs:simpleContent msdata:ColumnName="Column_Text" msdata:Ordinal="1">
      <xs:extension base="xs:string">
        <xs:attribute name="name" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>
[...]

I’m not constrained by this technique. I’m developing in C#.

Any help is appreciated.

@RichardTowers: I tried the following with the xml listed and received the error “There is an error in XML document (1, 2).”

Base.Root[] cars = null;
XmlSerializer serializer = new XmlSerializer(typeof(Base.Root[]));
XmlReader reader = XmlReader.Create(new StringReader(xml));
cars = (Base.Root[])serializer.Deserialize(reader);
reader.Close();

I have tried changing the classes you suggested with xmlElementAttribute decorators, adding “” into the beginning of the xmlString. I’ve also tried switching out StringReader with StreamReader to no avail. Any other suggestions?

  • 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-04T15:19:40+00:00Added an answer on June 4, 2026 at 3:19 pm

    What you want to do is not really how deserializing works. On the one side you have some XML, on the other a C# class. They need to look like each other for the serializer to do its job.

    You say you want a class like:

    class Models
    {
        int Chevy { get; set; } //65
        int Dodge { get; set; } //75
    }
    

    Firstly, I don’t think its possible to serialize your XML into something like this. But even if it was, what happens if your XML has Mercedes in instead? You can’t deserialize that bit because it’s not in your class.

    Basically your classes need to mimic your XML. You want something like:

    class Root
    {
        Company Company { get; set; }
        List<Model> ModelList { get; set; }
    }
    
    class Company
    {
        string Location { get; set; }
        string Name { get; set; }
    }
    
    class Model
    {
        string Name { get; set; }
        int Value { get; set; }
    }
    

    You can then reference your models by doing something like:

    // Get the value for Chevy:
    root.ModelList.Find(model => model.Name == "Chevy").Value;
    

    Edit: Here’s a paste with all of the code needed to do this: http://pastebin.com/Z3b3558Z. Sorry about the indentation, VisualStudio got a bit excited.

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

Sidebar

Related Questions

I have a table from a vendor application that stores some xml data into
I have some XML that I want to parse using the lxml method in
I have some xml files that contain text, which are displayed on my website.
I am parsing some XML that will have a link such as the following
I have some data in an XML element that looks like this: <?xml version=1.0
We have some UTF-8 XML being returned by a REST handler that contains urls.
I have some code that uses Open XML to open up a .docx file,
I have some data in a database that I need represented in an XML
I have an SSIS (2005) package that transforms some XML data and then imports
I have a simplistic app (just learning) that reads some XML data from a

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.