I start when it comes to XML. I created a simple XML file and tried to parse it and assign values to variables. It worked, but the method that I used made me wonder if there are better ways, more elegant, if you like, for this task. Whether there is a?
Here is my xml file:
<start> <record> <var1>hello</var1> <var2>world</var2> </record> <record> <var1>another</var1> <var2>one</var2> </record> </start>
Here is the method I used:
string var1 = "", var2 = ""; using(XmlReader r = XmlReader.Create(file)) { while(r.Read()) { if (r.MoveToContent() == XmlNodeType.Element) { if(r.Name == "record") { var1 = ""; var2 = ""; } else if(r.Name = "var1") var1 = r.ReadElementString(); else if(r.Name = "var2") var2 = r.ReadElementString(); } else if(r.MoveToContent() == XmlNodeType.EndElement && r.Name == "record") { Console.WriteLine(var1 + " " + var2); } } }
I prefer the XmlDocument route:
here's a great article about CodeProject:
http://www.codeproject.com/KB/cpp/parsefilecode.aspx
Have you tried Linq To XM L?
, , , :
XElement xmlData = XElement.Load("XmlFile.xml"); string var1, var2; foreach (XElement element in xmlData.Elements("record")) { var1 = element.Element("var1").Value; var2 = element.Element("var2").Value; Console.WriteLine(var1 + " " + var2); }
, deserialize.
, VB XML.
Dim doc = XDocument.Load(file) For Each element In doc...<record> Dim var1 = element.<var1>.Single() Dim var2 = element.<var2>.Single() Console.WriteLine(var1.Value & var2.Value) Next
- XmlDocument Xpath - (ish) , , , , . - ( ):
XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNodeList records = doc.SelectNodes("/start/record"); foreach(XmlNode n : records) { string var1 = n.SelectSingleNode("var1").InnerText; string var2 = n.SelectSingleNode("var2").InnerText; }
Serialize/DeSerialize. . .
XmlSerializer. XML- , , , . .
, XmlDocument, LINQ to XML XmlSerializer, . XmlReader, . , 4 , XML , , , , API.
XML, XmlSerializer, XML- . , . LINQ to XML .
XML , . , , xml. , Visual Studio XSD, XML . XSD Visual Studio. , :
: XSD path_to_your_xml.xml/o: your_output_directory XML.
, : XSD path_to_your_schema.xsd/c/l: cs/o: your_output_directory .cs , XML .
, , , . . . . , , , xml. "xsd/?" , .
. XML ?
- "Deserialize!, Deserialize!, Deserialize!"
XmlDocument (http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx).
Google: http://www.csharphelp.com/archives/archive199.html
Linq2Xml:
http://weblogs.asp.net/scottgu/archive/2007/08/07/using-linq-to-xml-and-how-to-build-a-custom-rss-feed-reader-with-it.aspx
, XmlDocument , . , .
http://support.softartisans.com/kbview.aspx?ID=673
Linq-way XDocument XElement. , XML.
ANTLR 2.x XML ANTXR. XML, , .
. http://javadude.com/tools/antxr/index.html.
/, .
- :
[Serializable] class record { string var1 { get; set; } string var2 { get; set; } }
, XmlArrayItemAttribute - MSDN.
!
Source: https://habr.com/ru/post/1710036/More articles:jQuery to re-enable a form after returning a file result - jqueryHow to partially transfer a database to a new system over time? - databaseAvailability of Abstract Properties - c #How can I get the main form of the Delphi IDE? - idevertical scrollbar doesn't work when using xp window theme (not classic) - javascriptWhat are popular code generation engines? - code-generationWhy is the jpg file that I converted from a gif file not clear and understandable? - javaWhy downloading more than one megabyte of images consumes all my iPhone memory? - memory-managementдвоичное дерево numNodes - c++UIDatePicker and NSDate - iphoneAll Articles