c# - I can never predict XMLReader behavior. Any tips on understanding? -
It seems that every time I use the XMLReader, I'm trying to understand a bunch of errors and error in what I am doing. About reading about what I am versus versus versus I just read what I always consider it at the end, but even then, after using it several times, it does not seem like what XMLReader really is doing When I call different functions. For example, when I read for the first time, if he reads an element start tag, is it now at the end of the element tag, or is it ready to begin reading the properties of the element? Does it know the values of properties, but if I call GetAttribute? What if I call ReadStartElement at this point? Will it finish reading the elementary element, or for the next one, will miss all the features? What if I want to read many elements - what is the best way to try to read the next element and to determine what its name is. Will the Aestart Element be read after work, or will I have to return the information about the node, I have read the element of the Astasteal element?
As you can see, I really do not understand where an XMLReader is in different stages, and how it is affected by the works taught by different states. Is there a simple pattern that I just failed to notice?
Another example of the problem (taken from the responses):
string input = "machine code = \" 01 \ "& gt; terminator" + "Part code = \" 01a \ "& gt; right arm & lt; / part & gt;" + "& lt; part code = \" 02 \ "& gt; left arm & lt; / part & Gt; " + "& Lt; part code = \" 03 \ "& gt; large toe & lt; / part & gt;" + "& Lt; / machine & gt;"; (Using System.IO.StringReader sr = new System.IO.StringReader (Input)) (Exlette Text Reader = New Extract Text Reader (SR)) {reader.WhitespaceHandling = WhitespaceHandling.None; Reader.MoveToContent (); While (reader.Read ()) {if reader.Name.Equals ("machine") & amp; amp; (reader.NodeType == XmlNodeType.Element)) {Console.Write ("Machine code {0} : ", Reader.GetAttribute (" code ")); Console.WriteLine (reader.ReadElementString ("Machine")); } If (reader.Name.Equals ("part") & amp; (reader.NodeType == XmlNodeType.Element)) {Console.Write ("Part Code {0}:", Reader. GetAttribute ("code")) ; Console.WriteLine (reader.ReadElementString ("share")); }}}}
First of all, the machine node is completely left. HantÅto content seems to be moving due to the content of the machine element, so that it can never be parsed. In addition, if you leave MoveToContent, you receive an error: "Element" is an invalid XmlNodeType. " Trying to read ReadStatString, which I can not quite explain.
The next problem is, while reading the first part element, after reading ReadElementString, the reader is at the beginning of the element at the beginning of the element. This causes the reader to happen. To leave the last element of the next part, read the beginning of the next loop to leave the next element. So the final output of this code is:
Part Code 01A: Right Arm
Part Code 03: Big toe
This is a major to confuse The example is the behavior of XMLReader that I am trying to understand.
Here's the thing ... I've written a lot of the serialization code (including xml processing), And I find myself in the same boat at Absolutely ; I have a very easy way of guiding, therefore: not
I will quickly use XmlWriter
as a way of writing XML, but I want to implement any other time to IXmlSerializable
Before choosing, I would like to walk on hot coals - I will just write a different DTO
and map that data; It also means that the schema ("Max", "WSDL" etc.) comes in free.