0

次のXMLタイプのファイルがあります

<root>
<info version_id=... product_id=... account_id=...>
<CRM>
<result key=... value=...>
<result key=... value=...>
....
</CRM>
<result key=... value=....>
</info>

<info version_id=... product_id=... account_id=...>
<CRM>
<result key=... value=...>
<result key=... value=...>
....
</CRM>
<result key=... value=....>
</info>
</root>

「info」ノードごとに次のリストを作成する必要があります。

だから私が望むクローズはこのコードでした

string[] directoryXml = Directory.GetFiles(directory);
var listxmlresult = new List<XmlResultNode>();

foreach (var xmlfile in directoryXml)
{
    var docxml = new XmlDocument();
    docxml.Load(xmlfile);
    XmlNodeList nodesInfo = docxml.SelectNodes("//info");
    XmlNodeList nodesResult = docxml.SelectNodes("//info/CRM/result");


    foreach (XmlNode info in nodesInfo)
    {

        string VersionId = info.Attributes["version_id"].Value;
        string ProductId = info.Attributes["product_id"].Value;
        string AccountId = info.Attributes["account_id"].Value;
        string AccountProductId = AccountId + " : " + ProductId;


        // verify that CRM node exist

        if (docxml.SelectSingleNode("//info/CRM") == null)
        {
            //To do log info that CRM node is not available for the specific file
        }

        foreach (XmlNode result in nodesResult)
        {
            //To do verfiy value is empty

            string Key = result.Attributes["key"].Value;
            string Value = result.Attributes["value"].Value;

            if (Value.Length != 0)
            {
                var listXmlResultTemp = new XmlResultNode(AccountProductId, Key, Value);
                listxmlresult.Add(listXmlResultTemp);
            }
        }
    }
}

ただし、このコードは、各「accountproduct」のすべての「キー」「値」を含むリストを返します。

2 番目のループで現在のノードを読み取るだけでは管理できません。いくつかの xpath の可能性を試しましたが、XmlNodeList で現在の位置を取得できません。

Tks

4

1 に答える 1

1

投稿されたように達成しようとしていることはあまり明確ではありません。このように内側のループを変更したいと思います:

foreach (XmlNode result in info.SelectNodes("./CRM/result"))
{
    string Key = result.Attributes["key"].Value;
    string Value = result.Attributes["value"].Value;
    .....
    .....
}
于 2014-08-05T13:53:15.067 に答える