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")); //Не работает } } 
  • And what have you tried? - rdorn
  • What I haven’t tried out anymore ... - dexploizer
  • OK. Only please transfer this from the answer to the question if this code is not the solution. To do this, there is a question to edit button - rdorn
  • No problem)))) - dexploizer
  • <AddressCode="1" is this a typo? XML does not allow this, the element name is only a name, if a value is needed, it is either on the attribute or in the body of the element - rdorn

1 answer 1

It can be a simple recursive walk. Adjust to your needs.

 static void Main() { XElement root = XElement.Load("<source file path>"); foreach(XElement sample in root.Descendants("Sample")) { RecursiveOutput(sample); } Console.Read(); } static void RecursiveOutput(XElement root) { foreach (XElement e in root.Elements()) { Console.Write($"{e.Name} "); foreach (XAttribute a in e.Attributes()) { Console.Write($"{a.Name} = {a.Value} "); } Console.WriteLine(); RecursiveOutput(e); } } 

If you are sure of the constancy of the structure, you can:

 static void FixedOutput(XElement root) { foreach (XElement e in root.Elements("Analysis")) { Console.Write($"{e.Name} "); foreach (XAttribute a in e.Attributes()) { Console.Write($"{a.Name}={a.Value} "); } foreach (XElement ce in e.Elements()) { foreach (XAttribute a in ce.Attributes()) { Console.Write($"{a.Name}={a.Value} "); } } Console.WriteLine(); } } 

In any case, you can iterate only the attributes of a specific element, since the attribute is not an independent unit (or in another way) of the XML markup.

  • I am very sorry, but what is root? - dexploizer
  • This is your "Sample" element that is in the code, but not in the XML sample. Or XDocument.Root . if you want to work with him, in general, no difference. - rdorn
  • Is it possible to directly address all attributes? I just need to make further actions with them. I can reach the attributes of Analysis, but I do not know how to do this with the other tags. The code in question is dexploizer
  • @YuriGo no, access to attributes is possible only through the element that contains them, because An attribute is not a standalone XML unit, unlike many other elements, such as element text or annotations. Although if you are sure of the unchanged structure of the file, then it can be hard-written in the code, now I will add. - rdorn
  • For me, the main thing is that the result be like this: Value 1, ...., Type 1, Text 1, Name 1, ..... then immediately with the next line Value 2, ...., Type 2, Text 2, Name 2 , ..... And it was displayed in the console approximately there Console.WriteLine ("Value" + val, "Type" + type, "Text" + txt, .....); - dexploizer