Pregunta

Cuando trato de código como este,

XPathNodeIterator it;

string selectStr = "Equipment/Group/item";
it = nav.Select(selectStr);
while (it.MoveNext())
{
    String strVal = "";
    if (it.Current.HasChildren)
    {
        strVal = it.Current.Value.ToString(); // get wrong value here. I want to get 'COM' and 'MD'
    }
}

Mi archivo XML como este

<Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <License licenseId="" licensePath=""/>
  <Group>
    <item>
      <key>COM</key>
      <value>
        <format>10</format>
        <value>0</value>
      </value>
    </item>
    <item>
      <key>MD</key>
      <value>
        <format>20</format>
        <value>test</value>
      </value>
    </item>
.....

He encontrado cuando se utiliza el método while al bucle de todo el archivo XML es muy conveniente.

Pero no sé cómo establecer la relación de obtener valor de elemento cuando he definido un XPathNodeIterator que el anterior?

En realidad, tengo que leer todo el archivo XML con la ayuda de XPathNodeIterator.

¿Fue útil?

Solución

Usted tendría que iniciar un nuevo iterador para cada nodo secundario seleccionando sólo los subelementos. XPathNodeIterator y XPath , en general, está más orientado a Extracción los datos de XML. Si desea leer todo el archivo XML, se debe considerar XmlReader (muy rápido, sólo hacia adelante) o XDocument (fácil de usar).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top