<test>
<child>
ceci est du texte
</child>
<child1>
</child1>
</test>
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionpublic class XMLtoCSV { // Get selector of product private string GetSelector(XPathNavigator nav, string selector) { string completeSelector = selector + "/*[1]"; XPathNodeIterator iterator = nav.Select(completeSelector); if (iterator.MoveNext()) return selector + "/" + this.GetSelector(nav, iterator.Current.Name); return selector; } // get count of childs node private int GetChildsCount(XPathNavigator nav, string selector) { string allChildsSelector = selector + "/*"; string itemSelector = selector; XPathNodeIterator allChildsIterator = nav.Select(allChildsSelector); XPathNodeIterator itemIterator = nav.Select(itemSelector); return allChildsIterator.Count / itemIterator.Count; } // Convert to CSV private List<string> ConvertToCSV(XPathNavigator nav, string selector, int childsCount) { List<string> list = new List<string>(); string allChildsSelector = selector + "/*"; XPathNodeIterator iterator = nav.Select(allChildsSelector); int i = 0; string s = ""; while (iterator.MoveNext()) { if (s == string.Empty) s = iterator.Current.Value; else s += ";" + iterator.Current.Value; i++; if (i !0 && i % 8 0) { list.Add(s); s = string.Empty; } } return list; } public void ParseXML() { XPathDocument xmlPathDoc = new XPathDocument("test.xml"); XPathNavigator nav = xmlPathDoc.CreateNavigator(); string selector = this.GetSelector(nav, ""); int count = this.GetChildsCount(nav, selector); List<string> list = this.ConvertToCSV(nav, selector, count); // missing : write list in file } }
XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; using (XmlReader r = XmlReader.Create(chemin, settings)) { r.ReadStartElement("catalog"); while (r.Name == "product") { XElement logEntry = (XElement)XNode.ReadFrom(r); string source = (string)logEntry.Element("ref_produit"); } r.ReadEndElement(); }