将具有多个重复节点的xml转换为字典

时间:2012-09-03 17:15:12

标签: c# xml dictionary xml-parsing

大家好帮帮我..

这是我的xml

<ns1:UpdateReserves
 xmlns:ns1="http://XLCapital.XLI.WIS.ClaimService/1.0"
 xmlns:ns2="http://schemas.datacontract.org/2004/07/XLCapital.XLGS.Integration.Core.Foundation"
 xmlns:ns0="http://XLCapital.XLI.WIS.ClaimService/Schemas/1.0"
 xmlns:ns3="http://XLCapital.XLGS.Integration.Core.Foundation/3.0">
<ns1:oReserves> <ns0:IntegrationProcessInfo>
<ns3:GlobalTrackingID>C553BF2BE7DAA91B</ns3:GlobalTrackingID>
<ns3:SystemOfOrigin>GCS</ns3:SystemOfOrigin>
<ns3:ParentMessageID>55d242d8-547e-4088-ab68-3a19fca2d7fe</ns3:ParentMessageID>
<ns3:IOPCorrelationID>7DDC4C25B33D3DED</ns3:IOPCorrelationID>
<ns3:IOPSequenceNumber>6</ns3:IOPSequenceNumber>
</ns0:IntegrationProcessInfo> <ns0:GetClaimHeader>
<ns0:GCSClaimNumber>0001602579</ns0:GCSClaimNumber>
<ns0:PolicyNumber>DPR9406003</ns0:PolicyNumber>
<ns0:PolicySystem>WINUS</ns0:PolicySystem>
<ns0:EntitySourceType>GCS</ns0:EntitySourceType>
<ns0:SourceClaimNumber> <ns3:KeyType>CLAIMID</ns3:KeyType>
<ns3:CommonKey>C446A11E20613C0D                    </ns3:CommonKey>
<ns3:SystemKey>05101617</ns3:SystemKey> </ns0:SourceClaimNumber>
<ns0:TransactionDate>2011-07-22T18:21:25.687</ns0:TransactionDate>
<ns0:UserId>A014440</ns0:UserId>
<ns0:WinsProdCode>GLP</ns0:WinsProdCode> <ns0:ProvisionalIndicator
xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</ns0:GetClaimHeader> <ns0:Reserves> <ns0:Reserve>
<ns0:FinancialTransactionId>3AAFF97D7D8DB7E9</ns0:FinancialTransactionId>
<ns0:ClaimantMappedKey> <ns3:KeyType>COVERAGEID</ns3:KeyType>
<ns3:CommonKey>AB0E091A59AC92DD                    </ns3:CommonKey>
<ns3:SystemKey>05101617-10</ns3:SystemKey> </ns0:ClaimantMappedKey>
<ns0:ReserveDetails> <ns0:ReserveDetail>
<ns0:FinancialTransactionDetailId>1B46392A1D5020F2</ns0:FinancialTransactionDetailId>
<ns0:TransactionType>I</ns0:TransactionType>
<ns0:ReserveAmount>0.000000</ns0:ReserveAmount> </ns0:ReserveDetail>
</ns0:ReserveDetails> </ns0:Reserve> <ns0:Reserve>
<ns0:FinancialTransactionId>F523CD61D5D61F29</ns0:FinancialTransactionId>
<ns0:ClaimantMappedKey> <ns3:KeyType>COVERAGEID</ns3:KeyType>
<ns3:CommonKey>AB0E091A59AC92DD                    </ns3:CommonKey>
<ns3:SystemKey>05101617-10</ns3:SystemKey> </ns0:ClaimantMappedKey>
<ns0:ReserveDetails> <ns0:ReserveDetail>
<ns0:FinancialTransactionDetailId>F7D6CB741A0115BC</ns0:FinancialTransactionDetailId>
<ns0:TransactionType>E</ns0:TransactionType>
<ns0:ReserveAmount>4741.000000</ns0:ReserveAmount>
</ns0:ReserveDetail> </ns0:ReserveDetails> </ns0:Reserve>
</ns0:Reserves> </ns1:oReserves> </ns1:UpdateReserves>

xml有多个重复节点。我需要在字典中添加节点名称和值作为键,值对,但字典不允许我添加重复键。那我该怎么办?

我不知道如何获取当前节点的计数,以便我可以将它们附加到节点名并将其添加为唯一键。

这是我的代码

public void BuildDataTable(XmlNode node)
{
    XmlNode nodWorking;

    if (node.NodeType == XmlNodeType.Element)
    {
        if (node.HasChildNodes)
        {
            nodWorking = node.FirstChild;
            while (nodWorking != null)
            {
                if (nodWorking.NodeType != XmlNodeType.Element)
                {
                    dictname.Add(node.ParentNode.Name.ToString() + node.Name.ToString(), node.InnerText);
                }

                BuildDataTable(nodWorking);
                nodWorking = nodWorking.NextSibling;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解正确......你在设计词典时遇到问题......

如果要在字典中存储多个值,可以使用以下内容:
1)将列表作为字典值

IDictionary<string,IList<string>> 

2)使用唯一标记存储您的所有值,例如逗号,标签符号等等

IDictionary<string,string> 

如果变化很小,您可以使用以下代码...下面的代码将为每个重复键设置逗号分隔值

public static void BuildDataTable(XmlNode node)
{
    XmlNode nodWorking;

    if (node.NodeType == XmlNodeType.Element)
    {
        if (node.HasChildNodes)
        {
            nodWorking = node.FirstChild;
            while (nodWorking != null)
            {
                if (nodWorking.NodeType != XmlNodeType.Element)
                {

                    if(!dictname.ContainsKey(node.ParentNode.Name.ToString() + node.Name.ToString()))
                    dictname[node.ParentNode.Name.ToString() + node.Name.ToString()] = node.InnerText;
                    else
                        dictname[node.ParentNode.Name.ToString() + node.Name.ToString()] = dictname[node.ParentNode.Name.ToString() + node.Name.ToString()] +","+node.InnerText;

                }

                BuildDataTable(nodWorking);
                nodWorking = nodWorking.NextSibling;
            }
        }
    }
}