简体   繁体   中英

Deserializing XML File with multiple element attributes - attributes are not deserializing

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.

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.

You are defining the attributes on TestElement while they should be on TestXML . Example:

@"<TestXML attr1=""MyAttr"" attr2=""1"">
      <TestElement>26</TestElement>
  </TestXML>"

I just ran a test serializing/deserializing your object and it seems to work fine

Test:

TestXML obj = new TestXML{ attr1 = "Attrib1", attr2 = "Attrib2", DateAdded = DateTime.Now, TestElement = 44};

XmlSerializer serializer = new XmlSerializer(typeof(TestXML));
using (FileStream stream = new FileStream(@"C:\StackOverflow.xml", FileMode.OpenOrCreate))
{
    serializer.Serialize(stream, obj);
}

using (FileStream stream = new FileStream(@"C:\StackOverflow.xml", FileMode.Open))
{
    TestXML myxml = (TestXML)serializer.Deserialize(stream);
}

all attributes deserialized ok.

Result:

在此输入图像描述

Xml:

<?xml version="1.0"?>
<TestXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" attr1="Attrib1" attr2="Attrib2" DateAdded="10/01/2013 9:46:23 a.m.">
  <TestElement>44</TestElement>
</TestXML>

As an additional note to the accepted answer. Make sure the xml element doesn't contain a nil="true" attribute like this:

<TestXML>
  <TestElement attr1="MyAttr" attr2="1" DateAdded="" xsi:nil="true">25</TestElement>
</TestXML>

From my experience the deserializer won't deserialize attributes of an element marked as null (nil).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM