2016-11-01 13 views
0

我想在一個XML閱讀陣列,但我的代碼不會返回結果讀陣列中的XML在C#

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> 

代碼:

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

由於我可以讀取數組?

+2

您需要使用名稱空間管理器 – Pawel

+0

我建議使用NewtonSoft和Linq將xml轉換爲json。您將最終得到一個易於使用的JArray節點.. –

+0

[如何在使用XPath選擇XML節點時忽略名稱空間](http://stackoverflow.com/questions/4402310/how-to -ignore名稱空間時-選擇的XML節點與 - 的xpath) – NineBerry

回答

0

問題是導入的命名空間。您可以忽略命名空間爲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); 
} 

或者你可以使用一個XmlNamespaceManagerexplained 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); 
} 
-1

起初,我覺得問題在 xDoc.LoadXml(內容);您嘗試: xDoc.Load(filePathXml);

其次,我認爲問題

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

你試試:

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

另一種解決方案是使用LINQ到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); 
} 

不要忘了把命名空間當地名稱的前面。