无法制作简单的xml文件修改程序?

时间:2017-11-18 14:06:40

标签: c# linq-to-xml

我有一些XML文件可以在结构<funding-source><institution-wrap>...</institution-wrap></funding-source>中包含一些节点 我想获取节点内的值(如果有的话)并将值与另一个XML文件匹配,即 funding_info.xml 的节点<skos>以及是否存在匹配获取其父节点<skd>的属性值然后 用<funding-source><institution-wrap>...</institution-wrap></funding-source>替换主XML文件的<funding-source><institution-wrap>...</institution-wrap><fundref-id>The attribute value found</fundref-id></funding-source> funding_info.xml 如下所示

<?xml version="1.0" encoding="UTF-8"?>
<item>
    <skd id="inst/10.1.3169">
        <skosl>
            <skos>NSF</skos>
        </skosl>
        <skosl>
            <skos>National Science Foundation</skos>
        </skosl>
        <skosl>
            <skos>Jatio Bigyan Songothon</skos>
        </skosl>
    </skd>
    <skd id="inst/10.1.4560">
        <skosl>
            <skos>Massachusetts Institute of Technology</skos>
        </skosl>
        <skosl>
            <skos>MIT</skos>
        </skosl>
        <skosl>
            <skos>Massachusetts Institute of Technology, USA</skos>
        </skosl>
    </skd>
    <skd id="inst/11.2.30213">
        <skosl>
            <skos>European Union</skos>
        </skosl>
        <skosl>
            <skos>European Union</skos>
        </skosl>
        <skosl>
            <skos>European Union FP7 Programme</skos>
        </skosl>
    </skd>
</item>

例如,如果我要修改的XML文件包含某些节点,如

<funding-source><institution-wrap>NSF</institution-wrap></funding-source>
<funding-source><institution-wrap>Caltech</institution-wrap></funding-source>
<funding-source><institution-wrap>Massachusetts Institute of Technology, USA</institution-wrap></funding-source>

输出应为

<funding-source><institution-wrap>NSF</institution-wrap><fundref-id>10.1.3169</fundref-id></funding-source>
<funding-source><institution-wrap>Caltech</institution-wrap></funding-source>
<funding-source><institution-wrap>Massachusetts Institute of Technology, USA</institution-wrap><fundref-id>10.1.4560</fundref-id></funding-source>

由于 funding_info.xml 中的任何<skos>节点均未找到Caltech,因此其值不变。 我不确定如何处理这个问题,但下面是我尝试过但中途卡住的事情

  static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(@"C:\Users\Desktop\my_sample.xml", LoadOptions.PreserveWhitespace);
            var x = doc.Descendants("funding-source").Elements("institution-wrap").Select(a => a.Value).ToArray();
            if (x.Any())
            {
                foreach (var cont in x)
                {
                    XDocument doc2 = XDocument.Load(@"C:\Users\Desktop\funding_info.xml",
                        LoadOptions.PreserveWhitespace);
                    var y = doc2.Descendants("skos").Ancestors("skosl").Ancestors("skd").Attributes("id")
                        .Select(a => a.Value);
                    if (doc2.Descendants("skos").Any().Value(cont))
                    {
                        var y = doc2.Descendants("skos").Ancestors("skosl").Ancestors("skd").Attributes("id")
                            .Select(a => a.Value).First();
............. ...................
............. ..................                        

                    }
                }
            }


            Console.ReadLine();
        }

1 个答案:

答案 0 :(得分:1)

读入您的funding_info.xml文件,并在机构名称和skd ID之间创建映射。然后,您可以查看所有资金来源元素并检查他们是否已经拥有该ID。如果没有,请查看该映射以查看它是否具有已知值。如果是,请添加id。

<root>
  <funding-source>
    <institution-wrap>NSF</institution-wrap>
    <fundref-id>10.1.3169</fundref-id>
  </funding-source>
  <funding-source>
    <institution-wrap>Caltech</institution-wrap>
  </funding-source>
  <funding-source>
    <institution-wrap>Massachusetts Institute of Technology, USA</institution-wrap>
    <fundref-id>10.1.4560</fundref-id>
  </funding-source>
</root>

这应该产生如下输出:

// ...
var skdIds = fundingDoc.Descendants("skd").Elements("skosl")
    .ToLookup(s => s.Element("skos").Value.ToUpperInvariant(), s => (string)s.Parent.Attribute("id"));
// ...
        var name = f.Element("institution-wrap").Value.ToUpperInvariant();
// ...

如果您想使此不区分大小写,请将查找的键全部置为大写或小写。

public abstract class Common {
  private String overlapfield1;
  private String overlapfield2
}

public class Request extends Common {
  private String requestField1;
  private String requestField2;
}

public class Response extends Common {
  private String responseField1;
  private String responseField2;
}