0

こんにちは、同じ要素がネストされたxmlがあります。再帰的です(うれしい!)

そのようです:

<MyRoot>
  <Record Name="Header" >
    <Field Type="Pattern" Expression=";VR_PANEL_ID,\s+" />
    <Field Name="PanelID" Type="Pattern" Expression="\d+"/>
    <Field Type="Pattern" Expression="," />
    <Field Name="ProductionDateTime" Type="Pattern" Expression=".+?(?=,)" />
    <Field Type="Pattern" Expression=".+?" />
  </Record>
  <Record Name="Body" MaxOccurs="0">
    <Record Name="Command"  Compositor="Choice">
      <Record Name="Liquid Status" >
        <Record Name="Header" >
          <Field Type="Pattern" Expression="i30100" />
          <Field  Name="DateTime" Type="Pattern" Expression="\d{10}"/>
        </Record>
        <Record Name="Data" MinOccurs="0" MaxOccurs="0">
          <Field Name="DeviceNum" Type="Pattern" Expression="\d\d" />
          <Field Name="Status" Type="Pattern" Expression="\d{4}" />
        </Record>
      </Record>
    </Record>
    <Record Name="Footer" >
      <Field Type="Pattern" Expression="&amp;&amp;[A-F0-9]" />
    </Record>
  </Record>
</MyRoot>

XmlReaderが の上に配置されたら、(この場合はand )MyRootの直接の子のみをループする方法はありますか。これらのノードの xml の読み取りを別のクラスに再帰的に委譲しています。MyRoot<Record Name="Header" ><Record Name="Body" MaxOccurs="0">

重複した質問を検討する前に、OP が の xpath 軸以外の孫やその他のノードセットについて尋ねていないことを確認してchildrenください。私はこの質問に完全に一致するものを見つけることができませんでしXmlReaderた.

私がやりたいのは、 を引き渡しXmlReader、子 xml を消費している子オブジェクトが次の子を取得するために必要な場所を指し示すようにすることです。それは甘いでしょう。

4

1 に答える 1

0

これがうまくいきました。申し訳ありませんが、100%一般的ではありませんが、アイデアが得られるかもしれません:

public void ReadXml(System.Xml.XmlReader reader)
{
    ReadAttributes(this, reader);
    reader.Read(); //advance
    switch (reader.Name) {
        case "Record":
        case "Field":
            break;
        default:
            reader.MoveToContent(); //skip other nodes
            break;
    }

    if (reader.Name == "Record") {
        do {
            LexicalRecordType Record = new LexicalRecordType();
            Record.ReadXml(reader);
            Records.Add(Record);
            //.Read() 
        } while (reader.ReadToNextSibling("Record")); //get next record
    } else if (reader.Name == "Field") {
        do {
            LexicalField Field = Deserialize(reader.ReadOuterXml, typeof(LexicalField));
            Add(Field);
        } while (reader.Name == "Field");
    }
}

public void ReadAttributes(object NonSerializableObject, System.Xml.XmlReader XmlReader)
{
    XmlReader.MoveToContent();
    for (int Index = 0; Index <= XmlReader.AttributeCount - 1; Index++) {
        XmlReader.MoveToAttribute(Index);
        PropertyInfo PropertyInfo = NonSerializableObject.GetType.GetProperty(XmlReader.LocalName);
        PropertyInfo.SetValue(NonSerializableObject, ConvertAttribute(XmlReader.Value, PropertyInfo.PropertyType), null);
    }
    XmlReader.MoveToContent();
}
于 2015-02-18T17:20:43.763 に答える