سؤال

I have a simple class:

public class SomeClass
{
    public int SomeInt { get; set; }
    public string SomeString { get; set; }

    [XmlArrayItem("AString")]
    public List<string> SomeStrings { get; set; }
}

I need to serialize instances of this class to a non-well formed xml (which I cannot change). If I serialize the class as it is, I get the following:

<?xml version="1.0" encoding="utf-8"?>
<SomeClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SomeInt>1234</SomeInt>
  <SomeString>Hello</SomeString>
  <SomeStrings>
    <AString>One</AString>
    <AString>Two</AString>
    <AString>Three</AString>
  </SomeStrings>
</SomeClass>

What I want to get is the following (the AString elements are not contained in a parent element):

<?xml version="1.0" encoding="utf-8"?>
<SomeClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SomeInt>1234</SomeInt>
  <SomeString>Hello</SomeString>
  <AString>One</AString>
  <AString>Two</AString>
  <AString>Three</AString>
</SomeClass>

I have tried various combinations of the Xml* attributes on the List property but it always wants to write the parent element (SomeStrings).

Is there a way I can modify the class to achieve the result I want, short of implementing the IXmlSerializable interface?

هل كانت مفيدة؟

المحلول

Try the [XmlElement] attribute instead:

public class SomeClass
{
    public int SomeInt { get; set; }
    public string SomeString { get; set; }

    [XmlElement]
    public List<string> SomeStrings { get; set; }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top