在某个父节点内添加一个新节点(从另一个xml文件获取值)?

时间:2018-03-22 08:46:00

标签: c# xml-parsing linq-to-xml

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

1)从给定路径的用户

获取所有xml文件

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

3)获取节点<institution>的值,并在节点<skosxl:literalForm xml:lang="...">内的database xml中搜索确切的值

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

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

以下是该文件的示例input filedesired output

我尝试过:

string pathToUpdatedFile = @"D:\test\Jobs";
var files=Directory.GetFiles(pathToUpdatedFile,"*.xml");
foreach (var file in files)
{
    var fundingDoc = XDocument.Load(@"D:\test\database.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.Parse(File.ReadAllText(file),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();

但它不起作用......有人可以帮忙......

2 个答案:

答案 0 :(得分:1)

见下面的代码:

AWS.config.loadFromPath('/path/')

答案 1 :(得分:0)

我认为这就是你要找的东西(修改后的jdweng&#39;代码一点点)

UglifyJsPlugin