使用LINQ to XML从平面XML文件转换为分层XML文件

时间:2012-05-09 04:12:54

标签: c# xml linq

使用C#和LINK to XML,这个“扁平”XML文件来自一个名为“xVar”的变量

   <vWorkflows>
      <vSection sectionTitle="Bars" sectionId="4">
        <vCategory catTitle="Between Visits" catRef="13">
          <type typeTitle="" typeRef="">
            <link linkNum="">
              <linkTitle>placeholder1</linkTitle>
              <linkSummary></linkSummary>
              <linkKeywords></linkKeywords>
              <pubDate>2012-05-01 00:00:00</pubDate>
              <lastUpdate>2012-05-18 00:00:00</lastUpdate>
            </link>
          </type>
        </vCategory>
      </vSection>
      <vSection sectionTitle="Pre-Visit" sectionId="1">
        <vCategory catTitle="Scheduling" catRef="4">
          <type typeTitle="" typeRef="">
            <link linkNum="">
              <linkTitle>placeholder2</linkTitle>
              <linkSummary></linkSummary>
              <linkKeywords></linkKeywords>
              <pubDate>2012-05-02 00:00:00</pubDate>
              <lastUpdate>2012-05-19 00:00:00</lastUpdate>
            </link>
          </type>
        </vCategory>
      </vSection>
      <vSection sectionTitle="Patient Visit" sectionId="2">
        <vCategory catTitle="Check-in" catRef="5">
          <type typeTitle="" typeRef="">
            <link linkNum="">
              <linkTitle>placeholder3</linkTitle>
              <linkSummary></linkSummary>
              <linkKeywords></linkKeywords>
              <pubDate>2012-05-03 00:00:00</pubDate>
              <lastUpdate>2012-05-20 00:00:00</lastUpdate>
            </link>
          </type>
        </vCategory>
      </vSection>
      <vSection sectionTitle="Patient Visit" sectionId="2">
        <vCategory catTitle="Check-in" catRef="5">
          <type typeTitle="" typeRef="">
            <link linkNum="">
              <linkTitle>placeholder4</linkTitle>
              <linkSummary></linkSummary>
              <linkKeywords></linkKeywords>
              <pubDate>2012-05-04 00:00:00</pubDate>
              <lastUpdate>2012-05-21 00:00:00</lastUpdate>
            </link>
          </type>
        </vCategory>
      </vSection>
      <vSection sectionTitle="Bars" sectionId="4">
        <vCategory catTitle="Registration" catRef="3">
          <type typeTitle="" typeRef="">
            <link linkNum="">
              <linkTitle>placeholder5</linkTitle>
              <linkSummary></linkSummary>
              <linkKeywords></linkKeywords>
              <pubDate>2012-05-05 00:00:00</pubDate>
              <lastUpdate>2012-05-22 00:00:00</lastUpdate>
            </link>
          </type>
        </vCategory>
      </vSection>
    </vWorkflows>

......需要最终看起来像

<workflows>
  <section sectionTitle="Bars" sectionId="4">
    <category catTitle="Between Visits" catRef="13">
      <type typeTitle="" typeRef="">
        <link linkNum="">
          <linkTitle>placeholder1</linkTitle>
          <linkSummary></linkSummary>
          <linkKeywords></linkKeywords>
          <pubDate>2012-05-01 00:00:00</pubDate>
          <lastUpdate>2012-05-18 00:00:00</lastUpdate>
        </link>
      </type>
    </category>
    <category catTitle="Registration" catRef="3">
      <type typeTitle="" typeRef="">
        <link linkNum="">
          <linkTitle>placeholder5</linkTitle>
          <linkSummary></linkSummary>
          <linkKeywords></linkKeywords>
          <pubDate>2012-05-05 00:00:00</pubDate>
          <lastUpdate>2012-05-22 00:00:00</lastUpdate>
        </link>
      </type>
    </category>
  </section>
  <section sectionTitle="Patient Visit" sectionId="2">
    <category catTitle="Check-in" catRef="5">
      <type typeTitle="" typeRef="">
        <link linkNum="">
          <linkTitle>placeholder3</linkTitle>
          <linkSummary></linkSummary>
          <linkKeywords></linkKeywords>
          <pubDate>2012-05-03 00:00:00</pubDate>
          <lastUpdate>2012-05-20 00:00:00</lastUpdate>
        </link>
      </type>
    </category>
    <category catTitle="Check-in" catRef="5">
      <type typeTitle="" typeRef="">
        <link linkNum="">
          <linkTitle>placeholder4</linkTitle>
          <linkSummary></linkSummary>
          <linkKeywords></linkKeywords>
          <pubDate>2012-05-04 00:00:00</pubDate>
          <lastUpdate>2012-05-21 00:00:00</lastUpdate>
        </link>
      </type>
    </category>
  </section>
  <section sectionTitle="Pre-Visit" sectionId="1">
    <category catTitle="Scheduling" catRef="4">
      <type typeTitle="" typeRef="">
        <link linkNum="">
          <linkTitle>placeholder2</linkTitle>
          <linkSummary></linkSummary>
          <linkKeywords></linkKeywords>
          <pubDate>2012-05-02 00:00:00</pubDate>
          <lastUpdate>2012-05-19 00:00:00</lastUpdate>
        </link>
      </type>
    </category>
  </section>
</workflows>

它与以下内容有关,但是我的大脑模糊不清试图获得第一个“分组依据”中的所有属性以及如何添加其他不可避免的分组。

   XDocument xDoc = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XComment("XML Source Data for Dial Flash"),
        new XElement("workflows",
            from sec in xVar.Elements("vSection")
            //group sec by (string)sec.Attribute("sectionTitle").Value,
            group sec by (string)sec.Attribute("sectionTitle").Value into gsec
            select new XElement("section", 
                new XAttribute("sectionTitle", gsec.Key)
            ) 
        )
    );

有更好的方法吗?如果在此过程中日期可以转换为MM / dd / yyyy,则可以获得奖励...

1 个答案:

答案 0 :(得分:0)

试试这个:

        var xVar = XElement.Load("XMLFile1.xml");

        var query = xVar.Elements("vSection").
            GroupBy(grp => (string)grp.Attribute("sectionTitle")).
            Select(grp => new XElement("section", grp.First().Attributes(),
                grp.Select(vsec => new XElement("category",
                    vsec.Element("vCategory").Attributes(),
                    vsec.Element("vCategory").Elements()))
                )
            )
        ;

        var xml = new XElement("workflows", query);