Login Login

CSharp:XML XPath

From Aino Wiki

Jump to: navigation, search

XPath

XmlDocument mioXML = new XmlDocument();
mioXML .LoadXml(pathFile); 
 
XmlNodeList listaNodi = mioXML.SelectNodes("/Autori/Nominativo");
foreach (XmlNode nodo in listaNodi)
{
  string cognome = nodo["Cognome"].InnerText;
  string nome = nodo["Nome"].InnerText;
  Console.WriteLine("Autore: {0} {1}", cognome , nome);
}
Applicato al seguente XML:
<?xml version='1.0'?>
<Autori>
    <Nominativo>
        <Nome>Giovanni</Nome>
        <Cognome>BIANCHINI</Cognome>
    </Nominativo>
    <Nominativo>
        <Nome>Leo</Nome>
        <Cognome>ROSSI</Cognome>
    </Nominativo>
</Autori>

Output:

Autore: BIANCHINI Giovanni
Autore: ROSSI Leo



Segue una documentazione della tecnologia attraverso esempio:

XmlDocument indexDoc = new XmlDocument();
indexDoc.Load(currentIndexFile);
 
 
XmlElement indexElementFound = null;
 
string xPathExpProducerCode = "//files.index/file[@Prod_ID=" + ParseXpathString(producerCode) + "]";
XmlNodeList indexElements = indexDoc.SelectNodes(xPathExpProducerCode);
 
if (indexElements != null 
	&& indexElements.Count > 0)   //Trovato almeno uno?
{
	foreach (XmlElement indexElement in indexElements)
	{
		if (indexElement.Attributes["Prod_ID"].Value.Equals(producerCode)) 
		{
			//trovato producerCode, se anche il brand corrisponde ho trovato la scheda tecnica
			string supplier_id = indexElement.Attributes["Supplier_id"].Value;
 
			// etc
		}
 
 
		// etc
 
		//================================================
		// ricerca  per EAN
		//================================================
		if (!String.IsNullOrEmpty(prodElement.EAN) && indexElementFound == null)
		{
			//string xPathExpProducerCode = "//ICECAT-interface/files.index/file/EAN_UPCS/EAN_UPC[@Value='" + prodElement.EAN + "']";
			string xPathExpProducerCode = "//files.index/file/EAN_UPCS/EAN_UPC[@Value='" + prodElement.EAN + "']";
			XmlNodeList indexElements = indexDoc.SelectNodes(xPathExpProducerCode);
			if (indexElements != null && indexElements.Count > 0)
			{
				string supplier_id = ((XmlElement)indexElements[0].ParentNode.ParentNode).Attributes["Supplier_id"].Value;
				if (CheckBrand(prodElement.Brand, prodElement.DataSheetSupplierBrandID, supplier_id, ref supplMappingDoc))
				{
					indexElementFound = (XmlElement)indexElements[0].ParentNode.ParentNode;     //Trovato !
				}
				// etc
			}
		}
 
		// etc
	}	
}

Usando Linq

Lettura di un XML usando un attributo.
using System.Xml.Linq;
using System.Linq;
...
XDocument doc = XDocument.Load("FileFullPath");
XElement itemFromIMOInfo = doc.Descendants("key")
                              .Where(t => (string)t.Attribute("key") == "IMO" + imoNumber)
                              .FirstOrDefault();

File XML sorgente:

<?xml version="1.0" encoding="UTF-8"?>
<OpcServerConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<DecoderConfig ObjectType="Map">
		<key key="#IMO2222222"      value=""/>
	</DecoderConfig>
</OpcServerConfig>



C#


Visual Studio

Author