xmlreader - XmlWriter - reading an attribute (quick question) -
I'm using it for my code, it outputs the XML file completely, but it's a ' = 'The mark adds element name though only one attribute is one of my elements
I think I can do something like this
if (reader.Getattribute! = "") // I have made that space, I'm not sure which will actually work with {Console.WriteLine ("& lt; {0} = {1}>,", reader .name, reader. GetAttribute ( "Name")); } Else {Console.WriteLine ("& lt; {0} & gt;", reader.Name); }
But is there a cleaner way for code?
My code (without optional solution)
using the system; Using System.Xml; Using System.IO; Using System.Text; Public Class Mainclass {Private Static Zero Main () {XmlWriterSettings Settings = New XmlWriterSettings (); Settings.Indent = true; XmlWriter w = XmlWriter.Create (@ "path \ test.xml", settings); W.WriteStartDocument (); W.WriteStartElement ("sections"); W.WriteStartElement ("class"); W.WriteAttributeString ("name", "EE999"); W.WriteElementString ("Class_Name", "Programming"); W.WriteElementString ("Teacher", "James"); W.WriteElementString ("Room_Number", "333"); W.WriteElementString ("ID", "2324324"); W.WriteEndElement (); W.WriteEndDocument (); W.flush (); W.Close (); XmlReader Reader = XmlReader.Create (@ "Path \ test.xml"); While (reader.Read ()) {switch (reader.NodeType) {case XmlNodeType.Element: Console.WriteLine ("& lt; {0} = {1} & gt;", Reader .Names, Reader. GetAttribute (" Name ")); break; Case XmlNodeType.Text: console.light line (reader.Value); break; Case XmlNodeType.CDATA: Console. WrightLine ("& lt; [CDATA [[0}]", reader value.); break; Case XmlNodeType.ProcessingInstruction: console.light line ("& lt ;? {0} {1}?", Reader .name, reader value); break; Case XmlNodeType.Comment: Console. WrightLine ("& lt;! - {0} ->", reader value.); break; Case XmlNodeType.XmlDeclaration: Console. WrightLine ("& lt ;? Xml version = '1.0'? & Gt;"); break; Case XmlNodeType.Document: break; Case XmlNodeType.DocumentType: Console. WrightLine ("& lt;! DOCTYPE {0} [{1}]", Reader., Name, Reader. break; Case XmlNodeType.EntityReference: Console. WrightLine (reader.Name); break; Case XmlNodeType.EndElement: Console. WrightLine ("& lt; / {0}>", reader.Name); break; }}}}
output
due to this line
case XmlNodeType Element: Console.lightline ("& lt; {0} = {1}>", reader .name, reader. GetAttribute ("name")); break;
Always writes '=' without checking
A rough fix:
Case XmlNodeType.Element: Console.WriteLine ( "& Lt; {0}", reader.Name); If (reader.HasAttributes) // Enter the attributes console. WrightLine ("& gt;"); break;
But why do you use XmlReader? It is cumbersome and useful when working with huge XML streams.
If you do not have a dataset >> 10 MB, then take a look at XDocument or XmlDocument
In your example, can be replaced by XMLRiter (Approximate):
using // System.Xml.Linq; New Gel = New Excellence ("Classes", New Excellence ("Class", New X-Attribute ("Name", "EE999"), New Excellence ("Class_name", "Programming"), New Exclamation "," James "))); Root.Save (@ "path \ test.xml"); Var doc = XDocument.Load (@ "path \ test.xml"); // Doctor now has an in-memory tree of XElement objects / which you can navigate and
and here
Comments
Post a Comment