如何使用(元素中的多元素)创建XML文档?

时间:2014-11-28 11:10:25

标签: c# xml

我尝试创建一个XML文件。但我并没有按照自己的意愿获得这些元素。我哪里错了?

我想要一个具有1..n Group元素的根元素,并且在Group-elements 1..n Item-elements中。你是怎样做的?

这是来源:

    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");
    }
}

这是我得到的结果:

<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>
... 

......这就是我想要的结果:

<?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>
... 

2 个答案:

答案 0 :(得分:2)

我认为您的问题是,当您的枚举中的“Jaar”和“Week”属性发生更改时,您只想创建并附加Groep节点。因此,您可以删除foreach块末尾的rootNode.AppendChild(subNode);。请尝试以下方法......

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

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

            rootNode.AppendChild(subNode);

        }
      ...
    }

答案 1 :(得分:1)

试试这个

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);
    }

将创建以下xml文件

<?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>