I have a trouble with reading string (xml structure) using xmlReader.
string xml = @" <Data> <Prices Id="16" Code="C3" > <Units> <Unit Id="17"/> </Units> </Prices> <Units> <Unit Id="1" IsActive="true" /> <Unit Id="2" IsActive="true" /> </Units> <Product Id="16" Code="C3" > <Names> <Name NameVersion="1" Name="C3 " /> </Names> <Units> <Unit Id="16"/> </Units> </Product> </Data> " How could I read only Element "Units" with child's id 1 and 2 ?
Actually I tried parsing this string into XDocument.
var root = XElement.Parse(xmlFile); var units = root.Elements("Units").FirstOrDefault(); and it works fine but I could work with really big xml and I don't want to parse all string into XDocument. I'm looking for method when I could load partly elements from a string. Also I tried do this using XmlTextReader but this not working in my case
using (XmlReader xmlTextReader = XmlReader.Create(new StringReader(xmlFile))) { if (xmlTextReader.ReadToFollowing(elementName)) { bool isOnNode = xmlTextReader.ReadToDescendant(elementName); while (isOnNode) { yield return XNode.ReadFrom(xmlTextReader) as XElement; if (!xmlTextReader.IsStartElement(elementName)) isOnNode = xmlTextReader.ReadToNextSibling(elementName); } } } Expected output: xElement =
<Units> <Unit Id="1" IsActive="true" /> <Unit Id="2" IsActive="true" /> </Units>
No comments:
Post a Comment