我正在从XML文件创建数据导入,以输入到我们的CMS中。一门课程最多可以有 9 个级别的模块,每个级别中可以有多个模块标题。
例如.XML
<test:modules>
<test:module>
<test:title>
<![CDATA[ Module title 1 ]]>
</test:title>
<test:content>
<![CDATA[ ]]>
</test:content>
<test:group>Year 1 Core Modules</lmu:group>
</test:module>
<test:module>
<test:title>
<![CDATA[ Module title 2 ]]>
</test:title>
<test:content>
<![CDATA[ ]]>
</test:content>
<test:group>Year 1 Core Modules</test:group>
</test:module>
<test:module>
<test:title>
<![CDATA[ Module title 1 ]]>
</test:title>
<test:content>
<![CDATA[ ]]>
</test:content>
<test:group>Year 2 Core Modules</test:group>
</test:module>
<test:module>
<test:title>
<![CDATA[ Module title 2 ]]>
</test:title>
<test:content>
<![CDATA[ ]]>
</test:content>
<test:group>Year 2 Core Modules</test:group>
</test:module>
<test:module>
<test:title>
<![CDATA[ Module title 1 ]]>
</test:title>
<test:content>
<![CDATA[ ]]>
</test:content>
<test:group>Year 3 Core Modules</test:group>
</test:module>
</test:modules>
我想做的是遍历每个项目,对于每个组,在每个"组"下保存一个 ul li 模块列表。
我想将其保存到 9x3 2D 数组中,以便我可以访问它以进行导入。
所以:
modulesArray[0, 0] would be "Year 1 Core Modules"
modulesArray[0, 1] would be "<ul><li>Module title 1<li><li>Module title 2<li></ul>
我的代码如下,但我不确定从这里开始:
protected override FeedCourse MapXmlNodeToEntity(XElement p)
{
var xmlResult = new XmlDocument();
xmlResult.LoadXml(p.ToString());
var test = p.ToString();
var xmlnsManager = new XmlNamespaceManager(xmlResult.NameTable);
xmlnsManager.AddNamespace("ns", "http://xcri.org/profiles/1.2/catalog");
xmlnsManager.AddNamespace("xcriTerms", "http://xcri.org/profiles/catalog/terms");
xmlnsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlnsManager.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
xmlnsManager.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
xmlnsManager.AddNamespace("dcterms", "http://purl.org/dc/terms/");
xmlnsManager.AddNamespace("credit", "http://purl.org/net/cm");
xmlnsManager.AddNamespace("mlo", "http://purl.org/net/mlo");
xmlnsManager.AddNamespace("courseDataProgramme", "http://xcri.co.uk");
xmlnsManager.AddNamespace("test", "http://www.test.com");
var elements = xmlResult.ChildNodes;
var code =
xmlResult.DocumentElement.SelectSingleNode(
"dc:identifier[@xsi:type="courseDataProgramme:internalID"]", xmlnsManager).InnerText;
var title = xmlResult.DocumentElement.SelectSingleNode("dc:title", xmlnsManager).InnerText;
var subject = xmlResult.DocumentElement.SelectSingleNode("dc:subject", xmlnsManager).InnerText;
String[,] modulesArray = new String[6, 3];
XmlNodeList modList = xmlResult.DocumentElement.SelectNodes("test:modules/test:module", xmlnsManager);
// var modulestest1 = modList.Item(1).InnerText;
int modulecount = 0;
int titlescount = modList.Count;
var previousModuleGroupTitle = "";
var modulecontenttitles = "";
foreach (XmlNode mn in modList)
{
var currentmodgroup = mn["test:group"].InnerText;
if (previousModuleGroupTitle == "")
{
modulecontenttitles += "<ul>";
modulecontenttitles += "<li>" + mn["test:title"].InnerText + "</li>";
}
if (previousModuleGroupTitle == mn["test:group"].InnerText)
{
modulecontenttitles += "<li>" + mn["test:title"].InnerText + "</li>";
}
else
{
modulecontenttitles += "</ul>";
modulecount++;
modulesArray[modulecount, 0] = modulecontenttitles;
modulecontenttitles = "";
modulecontenttitles += "<ul><li>" + mn["test:title"].InnerText + "</li>";
}
// modulesArray[modulecount, 0] = mn["test:title"].InnerText;
//modulesArray[modulecount, 1] = mn["test:content"].InnerText;
//modulesArray[modulecount, 2] = mn["test:group"].InnerText;
previousModuleGroupTitle = mn["test:group"].InnerText;
modulecount++;
}
modulesArray[0, 0].ToString();
}
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication78
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
string xml =
"<Root xmlns:test="abc">" +
"<test:modules>" +
"<test:module>" +
"<test:title>" +
"<![CDATA[ Module title 1 ]]>" +
"</test:title>" +
"<test:content>" +
"<![CDATA[ ]]>" +
"</test:content>" +
"<test:group>Year 1 Core Modules</test:group>" +
"</test:module>" +
"<test:module>" +
"<test:title>" +
"<![CDATA[ Module title 2 ]]>" +
"</test:title>" +
"<test:content>" +
"<![CDATA[ ]]>" +
"</test:content>" +
"<test:group>Year 1 Core Modules</test:group>" +
"</test:module>" +
"<test:module>" +
"<test:title>" +
"<![CDATA[ Module title 1 ]]>" +
"</test:title>" +
"<test:content>" +
"<![CDATA[ ]]>" +
"</test:content>" +
"<test:group>Year 2 Core Modules</test:group>" +
"</test:module>" +
"<test:module>" +
"<test:title>" +
"<![CDATA[ Module title 2 ]]>" +
"</test:title>" +
"<test:content>" +
"<![CDATA[ ]]>" +
"</test:content>" +
"<test:group>Year 2 Core Modules</test:group>" +
"</test:module>" +
"</test:modules>" +
"</Root>";
XElement root = XElement.Parse(xml);
XElement modules = root.Descendants().Where(x => x.Name.LocalName == "modules").FirstOrDefault();
XNamespace ns = modules.Name.Namespace;
var results = modules.Descendants(ns + "module")
.GroupBy(x => x.Element(ns + "title").Value)
.Select(y => new {
title = y.Key,
group = y.Select(z => (string)z.Element(ns + "group")).ToList()
})
.ToList();
}
}
}
谢谢你的回答。我认为对于其他需要此功能的项目,我将重新编写代码以使用您的方法。非常有用,所以感谢您的时间和精力。
对于这个特定的项目,我已经在逻辑上完成了排序,这完成了我需要它做的事情,为输入做好准备。希望这些答案中的任何一个都能帮助其他人解决同样的问题。
String[,] modulesArray = new String[6, 3];
XmlNodeList modList = xmlResult.DocumentElement.SelectNodes("test:modules/test:module", xmlnsManager);
int modulecount = 0;
int maxtitlescount = modList.Count;
int moduleArrayIndex = 0;
var previousModuleGroupTitle = "";
var modulecontenttitles = "";
foreach (XmlNode mn in modList)
{
var currentmodgroup = mn["test:group"].InnerText;
if (previousModuleGroupTitle == "")
{
modulecontenttitles += "<ul>";
modulecontenttitles += "<li>" + mn["test:title"].InnerText + "</li>";
}
else
{
if (previousModuleGroupTitle != currentmodgroup)
{
modulecontenttitles += "</ul>";
modulesArray[moduleArrayIndex, 0] = previousModuleGroupTitle;
modulesArray[moduleArrayIndex, 1] = modulecontenttitles;
modulecontenttitles = "";
moduleArrayIndex++;
modulecontenttitles += "<ul>";
}
else
{
modulecontenttitles += "<li>" + mn["test:title"].InnerText + moduleArrayIndex + "</li>";
modulecontenttitles += modulecount;
modulecontenttitles += maxtitlescount;
}
}
if((modulecount+1) == maxtitlescount)
{
modulecontenttitles += "</ul>";
modulesArray[moduleArrayIndex, 0] = currentmodgroup;
modulesArray[moduleArrayIndex, 1] = modulecontenttitles;
modulecontenttitles = "";
}
previousModuleGroupTitle = mn["test:group"].InnerText;
modulecount++;
}
var modules1title = (modulesArray[0, 0] != null) ? modulesArray[0, 0].ToString() : "";
var modules1content = (modulesArray[0, 1] != null) ? modulesArray[0, 1].ToString() : "";
var modules2title = (modulesArray[1, 0] != null) ? modulesArray[1, 0].ToString() : "";
var modules2content = (modulesArray[1, 1] != null) ? modulesArray[1, 1].ToString() : "";
var modules3title = (modulesArray[2, 0] != null) ? modulesArray[2, 0].ToString() : "";
var modules3content = (modulesArray[2, 1] != null) ? modulesArray[2, 1].ToString() : "";
var modules4title = (modulesArray[3, 0] != null) ? modulesArray[3, 0].ToString() : "";
var modules4content = (modulesArray[3, 1] != null) ? modulesArray[3, 1].ToString() : "";
var modules5title = (modulesArray[4, 0] != null) ? modulesArray[4, 0].ToString() : "";
var modules5content = (modulesArray[4, 1] != null) ? modulesArray[4, 1].ToString() : "";
var modules6title = (modulesArray[5, 0] != null) ? modulesArray[5, 0].ToString() : "";
var modules6content = (modulesArray[5, 1] != null) ? modulesArray[5, 1].ToString() : "";