本文介绍了如何使用C#Console Windows更新XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中重复4次,缺少2次。
我的要求是我想用缺少的单词自动替换重复的id。
因此我的ID应该没有任何遗漏ID。
希望你能理解我的问题
Here in the code below 4 is repeated and 2 is missing.
My requirement is that I want to automatically replace the repeated id with the missing word.
Hence My id should be ordered without any missing id.
Hoping you've understand my problem
<Model name="7049_PIPING.sat" version="v2.0" unit="m" count="12240" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Color id="256" r="255" g="255" b="255" />
<Component id="0" colorId="256" layer="0">
</Component>
<Component id="1" colorId="256" layer="0">
</Component>
<Component id="4" colorId="256" layer="0">
</Component>
<Component id="3" colorId="256" layer="0">
</Component>
<Component id="4" colorId="256" layer="0">
</Component>
<Component id="5" colorId="256" layer="0">
</Component>
<Component id="6" colorId="256" layer="0">
</Component>
<Component id="8" colorId="256" layer="0">
</Component>
<Component id="8" colorId="256" layer="0">
</Component>
<Component id="9" colorId="256" layer="0">
</Component>
<Component id="10" colorId="256" layer="0">
</Component>
</Model>
预期产量:
Expected Output:
<pre lang="xml"><Model name="7049_PIPING.sat" version="v2.0" unit="m" count="12240" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Color id="256" r="255" g="255" b="255" />
<Component id="0" colorId="256" layer="0">
</Component>
<Component id="1" colorId="256" layer="0">
</Component>
<Component id="2" colorId="256" layer="0">
</Component>
<Component id="3" colorId="256" layer="0">
</Component>
<Component id="4" colorId="256" layer="0">
</Component>
<Component id="5" colorId="256" layer="0">
</Component>
<Component id="6" colorId="256" layer="0">
</Component>
<Component id="7" colorId="256" layer="0">
</Component>
<Component id="8" colorId="256" layer="0">
</Component>
<Component id="9" colorId="256" layer="0">
</Component>
<Component id="10" colorId="256" layer="0">
</Component>
</Model>
推荐答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace Test_XmlChange
{
class Program
{
static void Main(string[] args)
{
try
{
Queue<XElement> duplicated = new Queue<XElement>();
Queue<int> missing = new Queue<int>();
XDocument d = XDocument.Load("XMLFile1.xml");
var cs = d.Descendants("all").First().Descendants().OrderBy(i =>
int.Parse(i.Attribute("id").Value)).ToList();
int lastId = -1;
int id;
for (int i = 0; i < cs.Count(); i++)
{
// cs[i].Attributes("id").First().Value = (i+1).ToString();
id = int.Parse(cs[i].Attribute("id").Value);
if (id == lastId)
{
if (missing.Count > 0)
cs[i].Attribute("id").Value = missing.Dequeue().ToString();
else
duplicated.Enqueue(cs[i]);
}
if (id - lastId > 1)
{
for (int j = lastId + 1; j < id; j++)
{
if (duplicated.Count > 0)
duplicated.Dequeue().Attribute("id").Value = j.ToString();
else
missing.Enqueue(j);
}
}
lastId = id;
}
d.Save("XMLFile_out.xml");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
/*
Filename: XMLFile1.xml
<?xml version="1.0" encoding="utf-8" ?>
<all>
<component id="0"> KK</component>
<component id="1"> KK</component>
<component id="1"> MM</component>
<component id="3"> QQ</component>
<component id="4"> WW</component>
<component id="4"> ZZ</component>
<component id="6"> LL</component>
</all>
*/
string szFilePath = @"D:\ABC.xml";//ur file path
XmlDocument objDOM = new XmlDocument();
objDOM.Load(szFilePath);//you can specify file name here
int iIndex = 0;
foreach (XmlNode objXNode in objDOM.GetElementsByTagName("Model")[0].ChildNodes)
{
if (objXNode.Name.Trim() == "Component")
{
objXNode.Attributes["id"].Value = (iIndex).ToString();
iIndex++;
}
}
objDOM.Save(szFilePath);
这篇关于如何使用C#Console Windows更新XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!