需要将2个XML中的属性合并为一个

时间:2012-05-01 09:51:21

标签: c# xml

我有两个像这样的XML。

XML1

<RPM>
  <CAI ID="101" Name="Guaranteed Payments"/>
  <CAI ID="102" Name="Sports Recreation"/> 
</RPM>

XML2

<RPM>
  <CAI ID="102" Active="False"/>
  <CAI ID="103" Active="True"/> 
</RPM>

我必须编写一个C#代码,它将根据CAI ID将两个XML的属性合并为一个。例如,两个XML都有一个CAI ID为102的节点,因此最终的XML将是

RESULT

    <RPM>
       <CAI ID="101" Name="Guaranteed Payments"/>
       <CAI ID="102" Name="Sports Recreation" Active="False"/>
       <CAI ID="103" Active="True"/>
    </RPM>

2 个答案:

答案 0 :(得分:2)

我很想做这样的事情,将两个XML文件反序列化为类,然后通过代码将它们组合起来。

因此XML定义类将是您想要的最终产品:

[XmlTypeAttribute]
[XmlRootAttribute("RPM")]
public class RPMConfiguration
{

    [XmlElementAttribute("CAI")]
    public CAI[] CAIList{ get; set; }


}

[XmlTypeAttribute]
public class CAI
{
    [XmlAttributeAttribute("ID")]
    public int ID { get; set; }

    [XmlAttributeAttribute("Name")]
    public string Name { get; set; }

    [XmlAttributeAttribute("Active")]
    public string Active{ get; set; }
}

然后你会像这样反序列化xml字符串:

public static object Deserialize(string xml)
{
    var deserializer = new System.Xml.Serialization.XmlSerializer(typeof(RPMConfiguration));
    using (var reader = XmlReader.Create(new StringReader(xml)))
    {
        return (RPMConfiguration)deserializer.Deserialize(reader);
    }
}

此时,您有两个RPMConfiguration对象,其中包含一个CAI对象列表,您可以在其中循环并匹配ID,将一个作为主集合处理,并复制缺少的任何属性。

一旦你完成了。只需将配置序列化为XML,然后就是一个完整的XML文件。

答案 1 :(得分:0)

string xml1 = @"
    <RPM>
        <CAI ID=""101"" Name=""Guaranteed Payments""/>
        <CAI ID=""102"" Name=""Sports Recreation""/> 
    </RPM>";

string xml2 = @"
    <RPM>
        <CAI ID=""102"" Active=""False""/>
        <CAI ID=""103"" Active=""True""/> 
    </RPM>";

XDocument xDoc1 = XDocument.Load(new StringReader(xml1));
XDocument xDoc2 = XDocument.Load(new StringReader(xml2));
var cais = xDoc1.Descendants("CAI")
          .Concat(xDoc2.Descendants("CAI"))
          .GroupBy(x => x.Attribute("ID").Value)
          .Select(x => x.SelectMany(y => y.Attributes()).DistinctBy(a => a.Name))
          .Select(x => new XElement("CAI", x));

string xml = new XElement("RPM", cais).ToString();

您可以在Jon Skeet here

中找到DistinctBy的完整实现
public static partial class MyExtensions
{
    public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)
    {
        HashSet<TKey> knownKeys = new HashSet<TKey>();
        foreach (T element in source)
        {
            if (knownKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }
}
相关问题