There is such an xml file
<Analysis Value="1" ReplyStatus="1" TestGroupCode="1"> <AnaComment Type="1" Text="1" /> <Lab AddressCode="1" Name="1" Address="1" /> </Analysis>
It is necessary to get the values of all attributes from <Analysis> to </Analysis> , including child elements. The number of attributes as in the code. The <Analysis></Analysis> may be repeated, but with different values.
<Analysis Value="1" ReplyStatus="1" TestGroupCode="1"> <AnaComment Type="1" Text="1" /> <Lab AddressCode="1" Name="1" Address="1" /> </Analysis> <Analysis Value="2" ReplyStatus="2" TestGroupCode="2"> <AnaComment Type="2" Text="2" /> <Lab AddressCode="2" Name="2" Address="2" /> </Analysis>
In this case, it is necessary to output not everything in a heap, but line by line, as if separating one Analysis from another. Gentlemen, I really hope for your help.
I tried it like this
XDocument xdoc = XDocument.Load(xmlFile); foreach (XElement phoneElement in xdoc.Element("Sample").Elements("Analysis")) { XAttribute Value = phoneElement.Attribute("Value"); XElement Lab = phoneElement.Element("Lab"); XElement AnaComment = phoneElement.Element("AnaComment"); if (Value != null && AddressCode != null && AnaComment != null) { Console.WriteLine("Value: {0}", Value.Value); Console.WriteLine("Lab: {0}", AddressCode.Value); Console.WriteLine("AnaComment: {0}", AnaComment.Value); } Console.WriteLine(); }
And so
XmlDocument doc = new XmlDocument(); doc.Load(xmlFile); XmlNodeList nodeList = doc.SelectNodes("//Sample/Analysis"); foreach (XmlElement node in nodeList) { string TestGroupCode = node.GetAttribute("TestGroupCode"), TestMethodCode = node.GetAttribute("TestMethodCode"), AnaName = node.GetAttribute("AnaName"), Value = node.GetAttribute("Value"), Unit = node.GetAttribute("Unit"), RefMin = node.GetAttribute("RefMin"), RefMax = node.GetAttribute("RefMax"), RefText = node.GetAttribute("RefText"), RefMark = node.GetAttribute("RefMark"), Finding = null; XmlNodeList anaCommentList = node.SelectNodes("//Sample/Analysis/AnaComment"); foreach (XmlElement childNode in anaCommentList) { //string Fin = childNode.GetAttribute("AnaComment"); Finding += childNode.GetAttribute("Text"); //Console.WriteLine(childNode.GetAttribute("Text")); //Не работает } }