0

これは私のXMLです:

<?xml version="1.0"?>
<formatlist>
<format>
    <formatName>WHC format</formatName>
    <delCol>ID</delCol>
    <delCol>CDRID</delCol>
    <delCol>TGIN</delCol>
    <delCol>IPIn</delCol>
    <delCol>TGOUT</delCol>
    <delCol>IPOut</delCol>
    <srcNum>SRCNum</srcNum>
    <distNum>DSTNum</distNum>
    <connectTime>ConnectTime</connectTime>
    <duration>Duration</duration>
</format>
<format>
    <formatName existCombineCol="1">Umobile format</formatName>   //this format
    <delCol>billing_operator</delCol>
    <hideCol>event_start_date</hideCol>
    <hideCol>event_start_time</hideCol>
    <afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss">  //node i want
        <name>ConnectdateTimeAFcombine</name>
        <combineDate>event_start_date</combineDate>
        <combineTime>event_start_time</combineTime>
    </afCombineName>
    <afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss">  //node i want
        <name>aaa</name>
        <combineDate>bbb</combineDate>
        <combineTime>ccc</combineTime>
    </afCombineName>
    <modifyPerfixCol action="add" perfix="60">bnum</modifyPerfixCol>
    <srcNum>anum</srcNum>
    <distNum>bnum</distNum>
    <connectTime>ConnectdateTimeAFcombine</connectTime>
    <duration>event_duration</duration>
</format>
</formatlist>

Umobile 形式の形式を見つけて、これらの 2 つのノードを反復処理したいと考えています。

<afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss">  //node i want
    <name>ConnectdateTimeAFcombine</name>
    <combineDate>event_start_date</combineDate>
    <combineTime>event_start_time</combineTime>
</afCombineName>
<afCombineName dateType="DateTime" format="dd/MM/yyyy HH:mm:ss">  //node i want
    <name>aaa</name>
    <combineDate>bbb</combineDate>
    <combineTime>ccc</combineTime>
</afCombineName>

2 つのノードのすべての子ノードを一覧表示します。結果は次のようになります。

ConnectdateTimeAFcombine,event_start_date,event_start_time.
aaa,bbb,ccc

これどうやってするの?

4

4 に答える 4

0

すべての XML 関連のトラバースについて、XPath 式の使用に慣れる必要があります。とても便利です。特定のケースでより簡単に実行できる場合でも、XPath を使用することをお勧めします。このように、ある時点でスキームが変更された場合、XPath 式を更新するだけで、コードが稼働します。

完全な例については、この記事をご覧ください。

于 2013-03-31T06:43:54.613 に答える
0

System.Xml 名前空間 API を System.Xml.XPath 名前空間 API と共に使用できます。これは、タスクを実行するのに役立つ簡単なアルゴリズムです。

  1. 以下の XPATH を使用して、文字列Umobile 形式を含むテキスト ノードをフェッチします。

    XmlNode umobileFormatNameNode = document.SelectSingleNode("//formatName[text()='Umobile フォーマット']");

  2. umobileFormatNameNode の親は、関心のあるノードになります。

    XmlNode formatNode = umobileFormatNameNode.ParentNode;

  3. このノードの子を取得します。

    XmlNodeList afCombineFormatNodes = formatNode.SelectNodes("afCombineName");

  4. afCombineFormatNodes のリストを処理できるようになりました

    for(XmlNode xmlNode in afCombineNameFormtNodes) { //プロセス ノード }

于 2013-03-31T06:44:53.370 に答える
0

このようにして、これらの要素にアクセスできます。

var doc = System.Xml.Linq.XDocument.Load("PATH TO YOUR XML FILE");
var result = doc.Descendants("format")
            .Where(x => (string)x.Element("formatName") == "Umobile format")
            .Select(x => x.Element("afCombineName"));

result次に、この方法を繰り返すことができます。

foreach (var item in result)
{
    string format = item.Attribute("format").Value.ToString();
    string name = item.Element("name").Value.ToString();
    string combineDate = item.Element("combineDate").Value.ToString();
    string combineTime = item.Element("combineTime").Value.ToString();
}
于 2013-03-31T06:51:23.040 に答える