如何将新节点添加到具有来自其他文件的特定值的文件? [英] How do I add a new node to a file with specific value from another file?

查看:66
本文介绍了如何将新节点添加到具有来自其他文件的特定值的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个包含以下步骤的程序:

1)从给定路径的用户获取所有 xml 文件

2)打开每个文件(如果有的话)并搜索节点< institution> ,其格式为< funding-source>< institution-wrap>< institution> ...< / institution>< / institution-wrap>< / funding-source>

3)获取节点< institution> 的值,并在数据库xml < skosxl:literalForm xml:lang =...>

4 )如果找到,则获取其父节点的属性值< skos:Concept rdf:about =...> 减去字符串 http://dx.doi.org/

5)添加节点< institution-id institution-id-type =fundref > < institution> 节点之后的xml文件中,其值为< funding -source>< institution-wrap>< institution> ...< / institution>< institution-id institution-id-type =fundref> rdf的值:about属性< / institution-id> < / institution-wrap>< / funding-source>

这是一个样本输入文件和该文件的所需输出



< b>我尝试了什么:



Hi, I'm trying to create a program having the following steps:
1) Get all xml files from a user given path
2) Open each of the files (if any) and search for nodes <institution> where it is in the format <funding-source><institution-wrap><institution>...</institution></institution-wrap></funding-source>
3) Get the value of the nodes <institution> and search the exact value in the database xml inside the nodes <skosxl:literalForm xml:lang="...">
4) If found, get the attribute value of its parent node <skos:Concept rdf:about="..."> minus the string http://dx.doi.org/
5) Add a node <institution-id institution-id-type="fundref"> in the xml file after the <institution> node with the value like <funding-source><institution-wrap><institution>...</institution><institution-id institution-id-type="fundref">VALUE of the rdf:about attribute</institution-id></institution-wrap></funding-source>
Here is a sample input file and the desired output for that file.

What I have tried:

string pathToUpdatedFile = @"D:\test\test2\Jobs\IEEE_sample - Copy.xml";
			var files=Directory.GetFiles(pathToUpdatedFile,"*.xml");
			foreach (var file in files)
			{
				var fundingDoc = XDocument.Load(@"D:\test\test2\Jobs\fundref.xml");
				XNamespace rdf=XNamespace.Get("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
				XNamespace skosxl = XNamespace.Get("http://www.w3.org/2008/05/skos-xl#");
				XNamespace skos=XNamespace.Get("http://www.w3.org/2004/02/skos/core#");
				
				var targetAtt = fundingDoc.Descendants(skos+"Concept").Elements(skosxl+"prefLabel")
					.ToLookup(s => (string)s.Element(skosxl+"literalForm"), s => (string)s.Parent.Attribute(rdf+"about"));
				XDocument outDoc = XDocument.Load(pathToUpdatedFile,LoadOptions.PreserveWhitespace);
				foreach (var f in outDoc.Descendants("funding-source").Elements("institution-wrap"))
				{
					if (f.Element("institution-id") == null)
					{
						var name = (string)f.Element("institution");
						var x = targetAtt[name].FirstOrDefault(); // just take the first one
						if (x != null)
							
							f.Add(new XElement("institution-id", new XAttribute("institution-id-type","fundref"),x.Substring(@"http://dx.doi.org/".Length)));
					}
					outDoc.Save(file);
				}
				
				Console.ReadLine();



但它没有做我上面描述的......有人可以帮忙吗?

此外,数据库xml文件中可能存在相同的< skosxl:literalForm xml:lang =...> 值(但是使用不同的attrib( rdf:about )值),在这种情况下,取第一个匹配的值并写一个txt文件,说该值在数据库中多次出现或者类似的东西只是为了通知用户是否在文件中插入了正确的值。


But it is not doing what I described above...can anyone help?
Also, there could be identical <skosxl:literalForm xml:lang="..."> values in the database xml file (but with different attrib (rdf:about) values), in that case take the value of the first match and write a txt file saying the value appears multiple times in the database or something like that just to notify the user if the correct value is inserted in the file.

推荐答案

1)将xml文件读入字符串

2)将xml反序列化为类

3)更新类

4)将类序列化为xml(字符串)

5)保存xml



使用VS中的Paste Special从xml / json获取类。
1) Read xml file into string
2) Deserialize the xml to a class
3) Update the class
4) Serialize the class back to xml (string)
5) Save xml

Use Paste Special in VS to get classes from xml / json.


这篇关于如何将新节点添加到具有来自其他文件的特定值的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆