2012-05-09 52 views
0

使用C#,并链接到XML,即来自一个变量这种 “扁平化” 的XML文件名为 “XVAR”转换为使用LINQ to XML

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

分层XML文件...需要最后看起来像个

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

+0

是使用XSLT出了问题? – yamen

回答

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); 
+0

太棒了,我可以在创建源XML文件时修复日期格式。 –