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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:27:32+00:00 2026-06-17T08:27:32+00:00

Using C# .Net 4 — XML Sample (Real sample has 6 attributes) <TestXML> <TestElement

  • 0

Using C# .Net 4 — XML Sample (Real sample has 6 attributes)

<TestXML>
  <TestElement attr1="MyAttr" attr2="1" DateAdded="">25</TestElement>
</TestXML>

For my class definition I have the following:

public class TestXML() {
   public TestXML() {}

   public int TestElement {get; set;}
   [XmlAttribute]
   public string attr1 {get; set;}
   [XmlAttribute]
   public string attr2 {get; set;}
   [XmlIgnore]
   public DateTime DateAdded {get; set;}
   [XmlAttribute("DateAdded")]
   public string dateadded {
      get{ return (DateAdded == null ? "" : DateAdded.ToString();}
      set{ if(!value.Equals("")) DateAdded = DateTime.Parse(value);}
   }
}

Now the code to deserialize:

string xml = "<TestXML><TestElement attr1=\"MyAttr\" attr2=\"1\" DateAdded=\"\">26</TestElement></TestXML>"
using (StringReader sr = new StringReader(xml)) {
   XmlSerializer serializer = new XmlSerializer(typeof(TestXML));
   TestXML myxml = (TestXML)serializer.Deserialize(sr);
}

Now the result we get is(viewing object in VS):

myxml
  attr1         |  null
  attr2         |  null
  TestElement   |  25

At a complete loss as to why the attributes will not deserialize.

  • 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-17T08:27:33+00:00Added an answer on June 17, 2026 at 8:27 am

    To do that you need two levels:

    [XmlRoot("TestXML")]
    public class TestXml {
        [XmlElement("TestElement")]
        public TestElement TestElement { get; set; }
    }
    
    public class TestElement {
        [XmlText]
        public int Value {get;set;}
    
        [XmlAttribute]
        public string attr1 {get;set;}
    
        [XmlAttribute]
        public string attr2 {get;set;}
    }
    

    Note that the > 26 < may cause problems too (whitespace); you may need that to be a string instead of an int.

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

Sidebar

Related Questions

Using .NET (in an SSIS package): I've got an XML string in memory which
I'm using net.sf.json.xml.XMLSerializer And I have this error 25 déc. 2012 18:20:38 net.sf.json.xml.XMLSerializer getType
Using .NET reflector, I find that the SpinLock structure has a lot of cases
Using .NET 4, C# Let's say I have class Info that extends CustomTypeDescriptor .
Using .Net 4, isn't it possible to include forward slash in route parameters or
Using .net 2.0 . I want to display a column in a DataGridView as
Using .Net and Oracle 11g - I've been returning dataTables from a procedure inside
Using .NET and the Word Interop I am programmatically creating a new Word doc
Using .NET's Office interop libraries, does anybody know the best way to convert back
using .NET 3.5. I have a function I want to make multithreaded calls to.

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.