-1

I've an xml document like this

<?xml version="1.0"?>
<data>
    <myassembly name="t1" folder="1">
        <myassembly name="t1.1" folder="0" />
        <myassembly name="t1.2" folder="0">
            <myassembly name="t1.2.1" folder="0"/>
        </myassembly>
        <myassembly name="t2" folder="0"/>
        <myassembly name="t3" folder="0">
            <myassembly name="t3.1" folder="0"/>
            <myassembly name="t3.2" folder="0"/>
        </myassembly>
    </myassembly>
</data>

And two classes to read the xml data:

class data{
    [XmlElement("myassembly")]
    MyAssembly myassembly;
}

class MyAssembly{

    [XmlAttribute("name")]
    string name;

    [XmlAttribute("folder")]
    string folder;

    [XmlArrayItem("myassembly")]
    MyAssembly[] myassembly;
}

I want to have this array list structure:

data:
    assembly:
        -name: t1
        -folder: 1
        -myassembly[4]:
            [0]-name: t1.1
            [0]-folder: 0
            [0]-myassembly: null
            [1]-name: t1.2
            [1]-folder: 0
            [1]-myassembly: [4]
                [0]-name: t1.2.1
                [0]-folder: 0
                [0]-myassembly: null
            [2]-name: t2
            [2]-folder: 0
            [2]-myassembly: null
            [3]-name: t3
            [3]-folder: 0
            [3]-myassembly: [2]
                [0]-name: t3.1
                [0]-folder: 0
                [0]-myassembly: null
                [1]-name: t3.2
                [1]-folder: 0
                [1]-myassembly: null

But: with my attributes, i can't get this array list. I hope, I have described it sufficiently.

regard raiserle

dbc
  • 104,963
  • 20
  • 228
  • 340
raiserle
  • 677
  • 8
  • 31
  • sorry, c#, i've updated this question. – raiserle Aug 15 '16 at 19:48
  • Some code that shows what you have tried that is not working would go a long way to helping. – jdoyle1983 Aug 15 '16 at 19:51
  • I have tried this with some ``Xml-Attributes`` – raiserle Aug 15 '16 at 19:53
  • 1
    Right, that is apparent from your classes provided. What you do not have is the code you use to open and deserialize the xml document, the code that uses those classes. – jdoyle1983 Aug 15 '16 at 20:00
  • 1
    See recursive code at following posting. I have recommended this code lots of times : http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Aug 15 '16 at 21:15

1 Answers1

1

Assuming you are using XmlSerializer as seems likely, you have the following issues:

  1. XmlSerializer only serializes public types and members. All of your types and members are private.

  2. The public MyAssembly[] myassembly member of MyAssembly needs to be marked with [XmlElement("myassembly")]. This indicates that the array should be serialized as a sequence of elements named <myassembly> without an outer container element. By default an outer container element is used when serializing a collection.

Thus your classes should be (converting public fields to properties):

public class data
{
    [XmlElement("myassembly")]
    public MyAssembly myassembly { get; set; }
}

public class MyAssembly
{
    [XmlAttribute("name")]
    public string name { get; set; }

    [XmlAttribute("folder")]
    public string folder { get; set; }

    [XmlElement("myassembly")]
    public MyAssembly[] myassembly { get; set; }
}

Then you can serialize and deserialize using these extension methods:

public static class XmlSerializationHelper
{
    public static T LoadFromXML<T>(this string xmlString)
    {
        using (StringReader reader = new StringReader(xmlString))
        {
            return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
        }
    }

    public static string GetXml<T>(this T obj, bool omitStandardNamespaces = false)
    {
        XmlSerializerNamespaces ns = null;
        if (omitStandardNamespaces)
        {
            ns = new XmlSerializerNamespaces();
            ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
        }           
        using (var textWriter = new StringWriter())
        {
            var settings = new XmlWriterSettings() { Indent = true, IndentChars = "    " }; // For cosmetic purposes.
            using (var xmlWriter = XmlWriter.Create(textWriter, settings))
                new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
            return textWriter.ToString();
        }
    }
}

Using them as follows:

var data = xmlString.LoadFromXML<data>();

Sample fiddle.

dbc
  • 104,963
  • 20
  • 228
  • 340