user1531040
user1531040

Reputation: 2291

How to create an XML document with (multi-elements in an element)?

I try to create an XML-file. But I didn't get the elements as I wanted. Where am I going wrong?

I want a Root-element with 1..n Group-elements and in the Group-elements 1..n Item-elements. How do you do that?

This is the source:

    public void CreateXmlFile(ref List<DataType> dataList)
    {
        XmlDocument document = new XmlDocument();
        XmlNode docNode = document.CreateXmlDeclaration("1.0", "UTF-8", null);
        document.AppendChild(docNode);

        XmlElement rootNode = document.CreateElement("Root");
        document.AppendChild(rootNode);

        int jaar = 0;
        int week = 0;

        foreach (DataType item in dataList)
        {
            XmlElement subNode = document.CreateElement("Groep");

            if (jaar != item.Jaar || week != item.Week)
            {
                jaar = item.Jaar;
                week = item.Week;

                XmlAttribute jaarAttribute = document.CreateAttribute("Jaar");
                jaarAttribute.Value = item.Jaar.ToString();
                subNode.Attributes.Append(jaarAttribute);

                XmlAttribute weekAttribute = document.CreateAttribute("Week");
                weekAttribute.Value = item.Week.ToString();
                subNode.Attributes.Append(weekAttribute);

                rootNode.AppendChild(subNode);
            }

            XmlElement itemNode = document.CreateElement("Item");
            subNode.AppendChild(itemNode);

            XmlAttribute idListAttribute = document.CreateAttribute("ID_List");
            if (Extension.IsNumeric(item.Notering))
                idListAttribute.Value = "S0";
            else
                idListAttribute.Value = "S1";
            itemNode.Attributes.Append(idListAttribute);
            ...

            rootNode.AppendChild(subNode);
        }

        document.Save(@"C:\temp\myDocument.xml");
    }
}

This is the result I get:

<Root>
  <Groep Jaar="1981" Week="1">
    <Item ID_List="S0" Notering="1" Naam="Example 1"  />
  </Groep>
  <Groep>
    <Item ID_List="S0" Notering="2" Naam="Example 3" />
  </Groep>
  <Groep Jaar="1981" Week="2">
    <Item ID_List="S0" Notering="1" Naam="Example X"/>
  </Groep>
  <Groep>
    <Item ID_List="S0" Notering="4" Naam="Example Y" />
  </Groep>
... 

... and this is thee result I wanna have:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
  <Groep Jaar="1981" Week="1">
    <Item ID_List="S0" Notering="1" Naam="Example 1"  />
    <Item ID_List="S0" Notering="2" Naam="Example 3" />
    ...
  </Groep>
  <Groep Jaar="1981" Week="2">
    <Item ID_List="S0" Notering="1" Naam="Example X"/>
    <Item ID_List="S0" Notering="4" Naam="Example Y" />
    ...
  </Groep>
... 

Upvotes: 0

Views: 1177

Answers (2)

maruthu chandrasekaran
maruthu chandrasekaran

Reputation: 168

Try this

public static void CreateXmlFile(IEnumerable<DataType> dataList, string xmlFileName)
    {
        XElement rootElement = new XElement("Root");

        foreach (var group in dataList.GroupBy(d=>new {d.Jaar, d.Week}))
        {
            XElement groepElement = new XElement("Groep");
            groepElement.SetAttributeValue("Jaar", group.Key.Jaar);
            groepElement.SetAttributeValue("Week", group.Key.Week);

            var items = group.Select(ge=>
                {
                    XElement itemElement = new XElement("Item");
                    itemElement.SetAttributeValue("ID_List", ge.ID_List);
                    itemElement.SetAttributeValue("Notering", ge.Notering);
                    itemElement.SetAttributeValue("Naam", ge.Naam);

                    return itemElement;
                });

            groepElement.Add(items);
            rootElement.Add(groepElement);
        }

        rootElement.Save(xmlFileName);
    }

will create the following xml file

<?xml version="1.0" encoding="utf-8"?>
<Root>
<Groep Jaar="1981" Week="1">
  <Item ID_List="S0" Notering="1" Naam="Example 1" />
  <Item ID_List="S0" Notering="2" Naam="Example 3" />
</Groep>
<Groep Jaar="1981" Week="2">
  <Item ID_List="S0" Notering="1" Naam="Example X" />
  <Item ID_List="S0" Notering="4" Naam="Example Y" />
</Groep>
</Root>

Upvotes: 1

twrowsell
twrowsell

Reputation: 467

I think your problem is that you only want to create and append a Groep node when "Jaar" and "Week" properties change in your enumeration. So you can remove rootNode.AppendChild(subNode); at the end of the foreach block. Try the following...

   ...
   XmlElement subNode = null;
    foreach (DataType item in dataList)
    {

        if (jaar != item.Jaar || week != item.Week)
        {
            subNode = document.CreateElement("Groep");
            ...

            rootNode.AppendChild(subNode);

        }
      ...
    }

Upvotes: 2

Related Questions