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?

3
  • 2
    You need to use namespace manager Commented Nov 1, 2016 at 2: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.. Commented Nov 1, 2016 at 2:25
  • Possible duplicate of How to ignore namespace when selecting XML nodes with XPath Commented Nov 1, 2016 at 2:37

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.