我希望在节点中找到相同值时合并节点,并且检查值是顺序还是不是
请检查以下几点:
1)如果code-id标签值是顺序方式且主题标签值相同则合并节点。
2)如果code-id标签值是顺序方式但主题标签值是diff,则避免合并。
3)如果code-id标签值不是序列化但主题标签值相同则避免合并。
4)如果code-id标签值不是序列化的,但主题标签值是diff,则避免合并。
您能否告诉我如何获得所需的输出?
提前致谢。
我的代码:
XDocument xDoc = XDocument.Parse(xmlstr);
XElement xElementSelectRoot = xDoc.XPathSelectElement("//root");
XElement xElementCreateRoot = new XElement(new XElement("root"));
IEnumerable<XElement> lstOFCatogory = xElementSelectRoot.XPathSelectElements("//category").OrderBy(r => r.Value).ToList();
foreach (var varXNodeCategory in lstOFCatogory)
{
if (varXNodeCategory.Parent.Name == "Product")
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(varXNodeCategory.Parent.ToString());
XmlNodeList xmlNodeCategoryList = xmlDoc.SelectNodes("//category");
XmlDocument xmlDoc1=new XmlDocument();
xmlDoc1.LoadXml(varXNodeCategory.ToString());
XmlNode xmlNodeSelectCategory = xmlDoc1.SelectSingleNode("//category");
foreach (XmlNode xmlNode in xmlNodeCategoryList)
{
xmlNode.ParentNode.RemoveChild(xmlNode);
if (xmlNode.FirstChild.InnerText.Equals(xmlNodeSelectCategory.FirstChild.InnerText))
{
xmlNodeSelectCategory = xmlNode;
}
}
xmlDoc.LastChild.AppendChild(xmlNodeSelectCategory);
XmlNode xmlNode1 = xmlDoc.SelectSingleNode("//Product");
var varXElement= XElement.Parse(xmlDoc.InnerXml);
xElementCreateRoot.Add(varXElement);
}
else
{
xElementCreateRoot.Add(varXNodeCategory);
}
}
............................................... .................................................. .................................................. .................. 输入: .................................................. .................................................. .................................................. ...............
<root>
<category>
<code-id type="pub">e00001</code-id>
<title>test334</title>
<ranking>240</ranking>
</category>
<Product>
<Product-subject-title>
<subject>BIOLOGY ARTICLE</subject>
</Product-subject-title>
<category>
<code-id type="pub">e00002</code-id>
<title>test34</title>
<ranking>160</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>IN BRIEF</subject>
</Product-subject-title>
<category>
<code-id type="pub">e00003</code-id>
<title>test6</title>
<ranking>117</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>IN BRIEF</subject>
</Product-subject-title>
<category>
<span>
<code-id type="pub">e00004</code-id>
</span>
<title>test3</title>
<ranking>52</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>BIOLOGY ARTICLE</subject>
</Product-subject-title>
<category>
<code-id type="pub">e00005</code-id>
<title>test28</title>
<ranking>10</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>IN BRIEF</subject>
</Product-subject-title>
<category>
<code-id type="pub">e00006</code-id>
<title>test4</title>
<ranking>116</ranking>
</category>
</Product>
<category>
<span>
<code-id type="pub">e00007</code-id>
</span>
<title>test76</title>
<ranking>14</ranking>
</category>
</root>
............................................... .................................................. .................................................. .................. 我的输出将如下所示: .................................................. .................................................. .................................................. ...............
<root>
<category>
<code-id type="pub">e00001</code-id>
<title>test334</title>
<ranking>240</ranking>
</category>
<Product>
<Product-subject-title>
<subject>BIOLOGY ARTICLE</subject>
</Product-subject-title>
<category>
<code-id type="pub">e00002</code-id>
<title>test34</title>
<ranking>160</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>IN BRIEF</subject>
</Product-subject-title>
<category>
<code-id type="pub">e00003</code-id>
<title>test6</title>
<ranking>117</ranking>
</category>
<category>
<span>
<code-id type="pub">e00004</code-id>
</span>
<title>test3</title>
<ranking>52</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>BIOLOGY ARTICLE</subject>
</Product-subject-title>
<category>
<code-id type="pub">e00005</code-id>
<title>test28</title>
<ranking>10</ranking>
</category>
</Product>
<Product>
<Product-subject-title>
<subject>IN BRIEF</subject>
</Product-subject-title>
<category>
<code-id type="pub">e00006</code-id>
<title>test4</title>
<ranking>116</ranking>
</category>
</Product>
<category>
<span>
<code-id type="pub">e00007</code-id>
</span>
<title>test76</title>
<ranking>14</ranking>
</category>
</root>
答案 0 :(得分:0)
我得到了解决方案,现在工作正常。
XDocument xmlDocNew = XDocument.Parse(xElementCreateRoot.ToString());
XElement xElementSelectRootNew = xmlDocNew.XPathSelectElement("//root");
XElement xElementCreateRootNew = new XElement(new XElement("root"));
IEnumerable<XElement> lstOFCatogoryNew = xElementSelectRootNew.XPathSelectElements("//category").OrderBy(r => r.Value).ToList();
List<Class1> lstClass1 = new List<Class1>();
int intClassIndexCounter = 0;
foreach (var x in lstOFCatogoryNew)
{
if (x.Parent.Name.ToString() == "Product")
{
if (lstClass1.Count() > 0)
{
if (lstClass1[lstClass1.Count()-1].SujectName.Trim().ToString() == ((XElement)x.Parent.FirstNode).Value.Trim().ToString())
{
var xsDoc = new XDocument(new XElement(XElement.Parse(x.Parent.ToString())));
XElement xElementSelectProduct = xsDoc.XPathSelectElement("//Product");
xElementCreateRootNew.Elements("Product").Last().Add(xElementSelectProduct.XPathSelectElement("//category"));
lstClass1.Add(new Class1(((XElement)x.Parent.FirstNode).Value.Trim().ToString(),//subject title
((XElement)x.FirstNode).Value.Trim().ToString(),//code-id
x.Parent.ToString().Trim()));//product set
intClassIndexCounter++;
}
else
{
xElementCreateRootNew.Add(x.Parent);
lstClass1.Add(new Class1(((XElement)x.Parent.FirstNode).Value.Trim().ToString(),//subject title
((XElement)x.FirstNode).Value.Trim().ToString(),//code-id
x.Parent.ToString().Trim()));//product set
intClassIndexCounter++;
}
}
else
{
xElementCreateRootNew.Add(x.Parent);
lstClass1.Add(new Class1(((XElement)x.Parent.FirstNode).Value.Trim().ToString(),//subject title
((XElement)x.FirstNode).Value.Trim().ToString(),//code-id
x.Parent.ToString().Trim()));//product set
intClassIndexCounter++;
}
}
else
{
xElementCreateRootNew.Add(x);
}
}