Lecture fichier Xml avec SelectNode et schema XSD
J ai un problème avec les ajouts de namespace qui concernent les schemas. Je ne parviens pas à utiliser les methods SelectNode à cause de ces déclarations.
J'ai certes trouvé un sujet similaire ici mais qui ne résoud pas mon pb.
Voici mon fichier xml:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8"?>
<AUTOSAR xmlns="http://autosar.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://autosar.org autosar.xsd">
<TOP-LEVEL-PACKAGES>
<AR-PACKAGE>
<SHORT-NAME>AUTOSAR</SHORT-NAME>
<SUB-PACKAGES>
<AR-PACKAGE>
<SHORT-NAME>Units</SHORT-NAME>
<ELEMENTS>
<UNIT>
<SHORT-NAME>percent</SHORT-NAME>
<DISPLAY-NAME>%</DISPLAY-NAME>
</UNIT>
</ELEMENTS>
</AR-PACKAGE>
</SUB-PACKAGES>
</AR-PACKAGE>
</TOP-LEVEL-PACKAGES>
</AUTOSAR> |
Et mon code src:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| this.xmlDoc = new XmlDocument();
this.xmlDoc.Load(xmlFilePath);
if (xmlDoc.DocumentElement.Name != "AUTOSAR")
throw new ArgumentException(xmlFilePath + " does not describe a proper AUTOSAR XML document.");
//Create an XmlNamespaceManager for resolving namespaces.
this.nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
this.nsmgr.AddNamespace(String.Empty, "http://autosar.org");
this.nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
this.nsmgr.AddNamespace("xsi:schemaLocation", "http://autosar.org autosar.xsd");
const string xpath = "/AUTOSAR/TOP-LEVEL-PACKAGES/AR-PACKAGE/SUB-PACKAGES/AR-PACKAGE";
XmlNodeList nodeList = xmlDoc.SelectNodes(xpath, nsmgr);
int count = nodeList.Count; |
En supprimant la déclaration des namespaces ds le fichier xml, mon code trouve bien un node. Mais avec, cela ne fonctionne tjs pas. Qqch doit manquer ds les AddNamespace, je ne vois pas quoi.
Une idée?