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();
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();
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.