首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用LINQ to XML将平面XML文件转换为分层XML文件

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

Stack Overflow用户
提问于 2012-05-09 12:12:54
回答 1查看 527关注 0票数 0

使用C#并链接到XML,这个“平面”XML文件来自一个名为"xVar“的变量。

代码语言:javascript
复制
   <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>

..。需要最终看起来像这样

代码语言:javascript
复制
<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>

它在某种程度上适用于以下内容,但我的大脑变得模糊,试图获得第一个"group by“中的所有属性,以及如何添加不可避免地随后的其他分组。

代码语言:javascript
复制
   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,则会获得奖励...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-09 16:38:10

试试这个:

代码语言:javascript
复制
        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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10509732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档