1

I'm trying to read arrays in an XML, but my code does not return results

XML :

<ArrayOfProductoModel
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://schemas.datacontract.org/2004/07/WebApi.Models">
    <ProductoModel>
        <descripcion>descripcion 1</descripcion>
        <fecha_registro>2016-03-01</fecha_registro>
        <id_producto>1</id_producto>
        <id_proveedor>1</id_proveedor>
        <nombre_producto>producto 1</nombre_producto>
        <precio>200</precio>
    </ProductoModel>
    <ProductoModel>
        <descripcion>descripcion 3</descripcion>
        <fecha_registro>2016-08-02</fecha_registro>
        <id_producto>3</id_producto>
        <id_proveedor>3</id_proveedor>
        <nombre_producto>producto 3</nombre_producto>
        <precio>500</precio>
    </ProductoModel>
</ArrayOfProductoModel>

Code :

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(content);
XmlNodeList nodelist = xDoc.SelectNodes("ArrayOfProductoModel/ProductoModel");
foreach (XmlNode node in nodelist) 
{
    MessageBox.Show(node.SelectSingleNode("descripcion").InnerText);
}

As I can read the array?

Mohit S
  • 13,723
  • 6
  • 34
  • 69
NemoBlack
  • 143
  • 1
  • 11
  • 2
    You need to use namespace manager – Pawel Nov 01 '16 at 02:24
  • I recommend using NewtonSoft and Linq to convert the xml to json. You'll end up with an JArray node which would be easy to work with.. – Keith John Hutchison Nov 01 '16 at 02:25
  • Possible duplicate of [How to ignore namespace when selecting XML nodes with XPath](http://stackoverflow.com/questions/4402310/how-to-ignore-namespace-when-selecting-xml-nodes-with-xpath) – NineBerry Nov 01 '16 at 02:37

3 Answers3

2

The problem is the imported namespace. You can ignore the namespace as explained here:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(content);

XmlNodeList nodelist = xDoc.DocumentElement.SelectNodes("*[local-name()='ProductoModel']");

foreach (XmlNode node in nodelist)
{
    MessageBox.Show(node.SelectSingleNode("*[local-name()='descripcion']").InnerText);
}

Alternatively you can use an XmlNamespaceManager as explained here:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(content);

XmlNamespaceManager manager = new XmlNamespaceManager(xDoc.NameTable);
manager.AddNamespace("MYNS", "http://schemas.datacontract.org/2004/07/WebApi.Models");

XmlNodeList nodelist = xDoc.DocumentElement.SelectNodes("MYNS:ProductoModel", manager);

foreach (XmlNode node in nodelist)
{
     MessageBox.Show(node.SelectSingleNode("MYNS:descripcion", manager).InnerText);
}
Community
  • 1
  • 1
NineBerry
  • 26,306
  • 3
  • 62
  • 93
0

Another solution is to use Linq to XML

var xml = @"<ArrayOfProductoModel
    xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""
    xmlns=""http://schemas.datacontract.org/2004/07/WebApi.Models"">
    <ProductoModel>
        <descripcion>descripcion 1</descripcion>
        <fecha_registro>2016-03-01</fecha_registro>
        <id_producto>1</id_producto>
        <id_proveedor>1</id_proveedor>
        <nombre_producto>producto 1</nombre_producto>
        <precio>200</precio>
    </ProductoModel>
    <ProductoModel>
        <descripcion>descripcion 3</descripcion>
        <fecha_registro>2016-08-02</fecha_registro>
        <id_producto>3</id_producto>
        <id_proveedor>3</id_proveedor>
        <nombre_producto>producto 3</nombre_producto>
        <precio>500</precio>
    </ProductoModel>
</ArrayOfProductoModel>";

 var xDoc = XDocument.Parse(xml);

 var ns = xDoc.Root.Name.Namespace;
 var nodelist = xDoc.Element(ns + "ArrayOfProductoModel").Elements(ns + "ProductoModel");

 foreach (var node in nodelist)
 {
      MessageBox.Show(node.Element(ns + "descripcion").Value);
 }

Don't forget to put namespace in front of local name.

Niyoko
  • 7,512
  • 4
  • 32
  • 59
-1

At first, I think problem in xDoc.LoadXml(content); You try: xDoc.Load(filePathXml);

Second, I think problem in

XmlNodeList nodelist = xDoc.SelectNodes("ArrayOfProductoModel/ProductoModel");
foreach (XmlNode node in nodelist) 
{
        MessageBox.Show(node.SelectSingleNode("descripcion").InnerText);
}

You try:

XmlNode rootNode = doc.SelectSingleNode(@"/ArrayOfProductoModel");
 var listProductModel = rootNode.SelectNodes(@"ProductoModel");
foreach (XmlNode node in listProductModel) 
{
    MessageBox.Show(node.SelectSingleNode("descripcion").InnerText);
}
binh
  • 1
  • 3