Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ to XML将平面XML文件转换为分层XML文件_C#_Xml_Linq - Fatal编程技术网

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

C# 使用LINQ to XML将平面XML文件转换为分层XML文件,c#,xml,linq,C#,Xml,Linq,使用C和linktoxml,这个平面XML文件来自一个名为xVar的变量 <vWorkflows> <vSection sectionTitle="Bars" sectionId="4"> <vCategory catTitle="Between Visits" catRef="13"> <type typeTitle="" typeRef=""> <link li

使用C和linktoxml,这个平面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>
有更好的方法吗?如果在此过程中可以将日期转换为MM/dd/yyyy,则可获得奖金…

尝试以下操作:

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

使用XSLT是不可能的吗?太棒了,我能够在创建源XML文件时修复日期格式。
   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)
            ) 
        )
    );
        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);