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 ...
是使用XSLT出了問題? – yamen