0

I have an xml structure like the following:

<Doc>
<Items>
<Foo A="1"/>
<Bar A="2"/>
</Items>
</Doc>

I would like to deserialize the xml into the following model:

[XmlRoot("Doc")]
public class MyDoc
{
    [XmlArray("Items")]
    // How do I select the element names
    public List<Item> Items { get; set; }
}

public class Item
{
     // How do I select the element name
     public string Name { get; set; }
}

I would want the Item.Name property to contain the element names: 'Foo', 'Bar'. Is this possible with XML attributes?

Bob
  • 821
  • 1
  • 17
  • 36

2 Answers2

0

In the nodeNames list, you will get All the elements Names

                string data = File.ReadAllText("D://readXML.txt");
                //From File i am reading XML
                XmlDocument xdoc = new XmlDocument();
                xdoc.LoadXml(data);
                XmlNodeList list = xdoc.SelectNodes("//Doc");
                List<string> nodeNames = new List<string>();

                foreach (System.Xml.XmlNode node in xdoc.SelectNodes("//Doc/Items"))
                {
                    foreach (System.Xml.XmlNode child in node.ChildNodes)
                    {
                        if (!nodeNames.Contains(child.Name)) nodeNames.Add(child.Name);
                    }
                }

Sample Using Attributes Reference Taken

XmlDocument doc = new XmlDocument();
doc.LoadXml("<reply success=\"true\">More nodes go here</reply>");

XmlElement root = doc.DocumentElement;

string s = root.Attributes["success"].Value;
Hitesh Anshani
  • 1,499
  • 9
  • 19
  • Each element in items is dynamic, so I won't know ahead of time which classes to create. – Bob Jul 03 '18 at 19:39
  • I need to deserialize the items to a strong typed class, I just need the class to contain a property that has the element name inside it. – Bob Jul 03 '18 at 19:50
  • It is a requirement to use xml attributes as stated in the original question. – Bob Jul 03 '18 at 20:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174289/discussion-between-d-john-anshani-and-bob). – Hitesh Anshani Jul 03 '18 at 20:04
0

I bet it's impossible do that with Xml attributes.

We'll have to do it manually. For example, by implementing the IXmlSerializable interface.

[XmlRoot("Doc")]
public class MyDoc : IXmlSerializable
{
    [XmlArray("Items")]
    public List<Item> Items { get; set; }

    public XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(XmlReader reader)
    {
        Items = new List<Item>();

        reader.ReadToFollowing("Items");

        using (var innerReader = reader.ReadSubtree())
        {
            innerReader.MoveToContent();

            while (innerReader.Read())
            {
                if (innerReader.IsStartElement())
                {
                    var item = new Item { Name = innerReader.Name };
                    Items.Add(item);
                }
            }
        }
    }

    public void WriteXml(XmlWriter writer)
    {
        throw new NotImplementedException();
    }
}

public class Item
{
    public string Name { get; set; }
}
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49