简体   繁体   中英

System.Xml.XmlException: ReadElementContentAs() methods cannot be called on an element that has child elements

I have looked at C# deserialize xml error ReadElementContentAs() methods cannot be called on an element that has child elements , but my situation seems to be different.

XML I'm trying to work with looks something like:

<SampleXML>
  <SomeElement1>SomeValue1</SomeElement1>
  <SomeElement2>SomeValue2</SomeElement2>
  <RepetitiveElements>
    <Id>SomeId1</Id>
    <Value>SomeValue1</Value>
  </RepetitiveElements>
  <RepetitiveElements>
    <Id>SomeId2</Id>
    <Value>SomeValue2</Value>
  </RepetitiveElements>
  <RepetitiveElements>
    <Id>SomeId3</Id>
    <Value>SomeValue3</Value>
  </RepetitiveElements>
</SampleXML>

And the class, that wrote with C# is this:

    [Serializable]
    public class MyClass
    {
        [XmlElement("SomeValue1", Namespace = "")]
        public string SomeValue1 { get; set; }

        [XmlElement("SomeValue2", Namespace = "")]
        public string SomeValue2 { get; set; }

        [XmlElement("RepetitiveElements", Namespace = "")]
        public List<RepetitiveElements> RepetitiveElements { get; set; }
    }

    [Serializable]
    public class RepetitiveElements
    {
        [XmlElement("Id", Namespace = "")]
        public string Id { get; set; }

        [XmlElement("Value", Namespace = "")]
        public string Value { get; set; }
    }

What I'm trying to achieve here, is having all RepetitiveElements from XML in MyClass.RepetitiveElements list. Problem is that deserializing is ending up with "System.Xml.XmlException: ReadElementContentAs() methods cannot be called on an element that has child elements." exception.

Any ideas or suggestions are appreciated.

This code works with sample xml posted

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication166
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
            MyClass myClass = (MyClass)serializer.Deserialize(reader);
        }
    }
    [XmlRoot("SampleXML")]
    public class MyClass
    {
        [XmlElement("SomeValue1", Namespace = "")]
        public string SomeElement1 { get; set; }

        [XmlElement("SomeElement2", Namespace = "")]
        public string SomeValue2 { get; set; }

        [XmlElement("RepetitiveElements", Namespace = "")]
        public List<RepetitiveElements> RepetitiveElements { get; set; }
    }

    [Serializable]
    public class RepetitiveElements
    {
        [XmlElement("Id", Namespace = "")]
        public string Id { get; set; }

        [XmlElement("Value", Namespace = "")]
        public string Value { get; set; }
    }
}

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