301228 How to: Read the XML data of the data stream (from mkba) in the .NET Framework SDK

xiaoxiao2021-03-06  74

The release number of this article has been CHS301228

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.xml system.io

This task content

summary

Require how to read XML data reference in data streams

Summary This article demonstrates how to use

The XMLTextReader class reads the extended tag language (XML) from the stream. The stream can come from various sources, such as from servers, files or

The byte stream of the TextReader class.

Back to top

Requirements The recommended hardware, software, network structure, and service pack required:

Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:

XML Terminology Create and Read XML

How to read XML data in the data stream

Open Visual Studio .NET. New Microsoft Visual Basic (VB) or Microsoft Visual C # console application. Note: The following steps provide a detailed description of how to generate an application. You can also directly go directly to step 10, and a complete code is provided in this step. Make sure the project contains references to System.xml and System.io namespace. Use the Imports statement for the XML namespace, so that there is no need to define the XMLTextReader declaration in the namespace in the later code. The Imports statement must be as follows before all other declarations. Visual Basic .NET Code Imports System.xml

Imports System.iovisual C # code Using System.xml;

Using system.io; create or retrieve XML data streams. The stream is an abstract concept that represents an input or output device for use as data (here XML data) source or target. You can write a stream or read from the stream, and the flow can be most imagerable to the flow of bytes. Data flow is used independently of the device, so if the source of the data stream changes, the program is not required to change. There are several different ways to create streams to the XMLTextReader class. Select One of the following code examples to the main process:

Using the code example of the StringReader object: StringReader object reads characters from the string, get a string value during the construction process. Visual Basic .NET code DIM stream as system.io.stringReader

stream = new stringReader ("" & _

"" & _

"& _

" "& _

The autobiography of benjamin franklin </ title> "& _</p> <p><author> "& _</p> <p>"<first-name> Benjamin </ first-name>" & _ "<last-name> FRANKLIN </ last-name>" & _</p> <p></ author> "& _</p> <p><price> 8.99 </ price> "& _</p> <p></ book> "& _</p> <p>"</ bookstore>") C # code StringReader Stream;</p> <p>stream = new stringReader ("<? xml version = '1.0'?>" </p> <p>"<! File Repesents a fragment of a book store inventory database. -> </p> <p>"<bookstore>" </p> <p><Book genre = / "autobiography /" publicationdate = / "1981 /" isbn = / "1-861003-11-0 /"> </p> <p>"<title> The autobiography of benjamin franklin </ title>" </p> <p>"Author>" </p> <p>"<first-name> Benjamin </ first-name>" </p> <p><Last-Name> Franklin </ last-name> </p> <p></ author> " </p> <p><price> 8.99 </ price> " </p> <p>"</ book>" </p> <p>"<book genre = /" novel / "publicationdate = /" 1967 / "isbn = /" 0-201-63361-2 / "> </p> <p><title> The confidence man </ title> " </p> <p>"Author>" </p> <p>"<first-name> Herman </ first-name>" </p> <p><Last-Name> Melville </ last-name> </p> <p></ author> " </p> <p><price> 11.99 </ price> " </p> <p>"</ book>" </p> <p><Book genre = / "philosophy /" publicationdate = / "1991 /" ISBN = / "1-861001-57-6 /"> " </p> <p>"<title> the gorgias </ title>" </p> <p><author> "<name> Plato </ name>" </p> <p></ author> " </p> <p>"Price> 9.99 </ price>" </p> <p>"</ book>" </p> <p>"</ BookStore>"); Use the code example of the StreamReader object: StreamReader object is used to read characters from the file. During the construction, the object reads the name of the file to be read: Visual Basic .NET code DIM stream as system.io.streamReader</p> <p>'Loadings the xml data in the file books.xml in a new stream.</p> <p>Stream = New StreamReader ("Books.xml") C # code system.io.streamReader stream = new system.io.StreamReader ("Books.xml"); please note that you use the books.xml file here. You can create your own books.xml file. Example books.xml files are provided with Visual Studio .NET and .NET Framework SDK. Use a data stream to construct an XMLTextReader class. Typically, if you need to use XML as the original data to be accessed without using the Document Object Model (DOM), you can use XMLTextReader; therefore, XMLTextReader provides a mechanism for reading XML faster. XMLTextReader uses a different constructor to specify the location of XML data. The following code loaded from a stream XMLTextReader: Visual Basic .NET code DIM Reader as XmlTextReader = New XMLTextReader (Stream) C # code XMLTextReader Reader = NULL;</p> <p>Reader = New XMLTextReader (Stream); Read all XML data. After loading, XMLTextReader will read XML data continuously and use the Read method to obtain the next record. After arriving at the last record, return false. Visual Basic .NET code do while (ready)</p> <p>'Do some work here on the data.</p> <p>Console.writeline (Reader.Name)</p> <p>Loop</p> <p>'Reading of the xml file has finished.</p> <p>Console.readline () 'Pausec # code while (reader.read ())</p> <p>{</p> <p>// Do some work here on the data.</p> <p>...</p> <p>}</p> <p>While (Reader.Read ())</p> <p>{</p> <p>// Do some work here on the data.</p> <p>Console.writeLine (Reader.Name);</p> <p>}</p> <p>Console.readline (); check node. To process XML data, each record should have a node type, which can determine the node type through the NodeType property. Name and Value Attributes Returns the node name (element and attribute name) of the current node (or record) and node value (node ​​text). NodeType enumerates determines the node type. The following example shows the name and document type of the element. Note that this example ignores the element properties. Visual Basic .NET code do while (reader.Read ()) Select Case Reader.NodeType</p> <p>Case XMLNodetype.ement 'Display Beginning of Element.</p> <p>Console.write ("<" Reader.name)</p> <p>Console.writeline (">")</p> <p>Case XMLNodType.text 'Display The Text in Each Element.</p> <p>Console.writeline (Reader.Value)</p> <p>Case XMLNodetype.Endelement 'Display end of element.</p> <p>Console.write ("</" ready.name)</p> <p>Console.writeline (">")</p> <p>End SELECT</p> <p>Loopc # code while (reader.Read ())</p> <p>{</p> <p>Switch (Reader.NodeType)</p> <p>{</p> <p>Case XMLNodetype.Element: // The node is an element.</p> <p>Console.write ("<" Reader.Name);</p> <p>Console.writeline (">");</p> <p>Break;</p> <p>Case XMLNodType.Text: // Display The Text in Each Element.</p> <p>Console.writeline (Reader.Value);</p> <p>Break;</p> <p>Case XMLNodetype.Endelement: // Display end of element.</p> <p>Console.write ("</" ready.name;</p> <p>Console.writeline (">");</p> <p>Break;</p> <p>}</p> <p>} Check the properties. Element node types can include a series of attribute nodes associated therewith. The MoveTonextAttribute method is moved in each attribute of the element. Use the Hasattribute attribute to detect whether the node has any properties. AttributeCount properties Returns the number of properties of the current node. Visual Basic .NET code do while (ready)</p> <p>Select Case Reader.NodeType</p> <p>Case XMLNodetype.ement 'Display Beginning of Element.</p> <p>Console.write ("<" Reader.name)</p> <p>If Reader.haSattributes Then 'if attributes exist</p> <p>While reader.movetonextAtttribute ()</p> <p>'Display Attribute Name and value.</p> <p>Console.write ("{0} = '{1}'", reader.name, reader.value)</p> <p>End while</p> <p>END IF</p> <p>Console.writeline (">")</p> <p>Case XMLNodType.text 'Display The Text in Each Element.</p> <p>Console.writeline (Reader.Value)</p> <p>Case XMLNodetype.Endelement 'Display end of element.</p> <p>Console.write ("</" ready.name)</p> <p>Console.writeline (">")</p> <p>End SELECT</p> <p>Loopc # code while (reader.Read ())</p> <p>{</p> <p>Switch (Reader.NodeType)</p> <p>{</p> <p>Case XMLNodetype.Element: // The node is an element.</p> <p>Console.write ("<" Reader.Name);</p> <p>While (Reader.MovetonextAttRibute ()) // read attributes.</p> <p>Console.write ("" Reader.Name "= '" Reader.Value "');</p> <p>Console.write (">");</p> <p>Console.writeline (">");</p> <p>Break;</p> <p>Case XMLNodType.Text: // Display The Text in Each Element.</p> <p>Console.writeline (Reader.Value);</p> <p>Break;</p> <p>Case XMLNodetype.Endelement: // Display end of element.</p> <p>Console.write ("</" ready.name;</p> <p>Console.writeline (">");</p> <p>Break;</p> <p>}</p> <p>} For convenience, this will provide a complete code. Visual Basic.net code IMPORTS System.xml</p> <p>Imports system.io</p> <p>Module Module1</p> <p>Sub main ()</p> <p>Dim Stream as system.io.streamReader</p> <p>'Loadings the xml data in the file books.xml in a new stream.</p> <p>Stream = New StreamReader ("Books.xml")</p> <p>Dim Reader As XmlTextReader = New XMLTextReader (Stream)</p> <p>Do while (reader.read ())</p> <p>Select Case Reader.NodeType</p> <p>Case XMLNodetype.ement 'Display Beginning of Element.</p> <p>Console.write ("<" Reader.name)</p> <p>If Reader.haSattributes Then 'if attributes exist</p> <p>While reader.movetonextAtttribute ()</p> <p>'Display Attribute Name and value.</p> <p>Console.write ("{0} = '{1}'", reader.name, reader.value)</p> <p>End while</p> <p>END IF</p> <p>Console.writeline (">")</p> <p>Case XMLNodType.text 'Display The Text in Each Element.</p> <p>Console.writeline (Reader.Value) Case XMLNodetype.Endelement 'Display End of Element.</p> <p>Console.write ("</" ready.name)</p> <p>Console.writeline (">")</p> <p>End SELECT</p> <p>Loop</p> <p>End Sub</p> <p>End modulec # code using system;</p> <p>USING SYSTEM.XML;</p> <p>Using system.io;</p> <p>Namespace ReadXmlFromstream</p> <p>{</p> <p>/// <summary></p> <p>/// summary description for class1.</p> <p>/// </ summary></p> <p>Class class1</p> <p>{</p> <p>Static void main (string [] args)</p> <p>{</p> <p>System.io.StreamReader stream = new system.io.streamReader ("Books.xml");</p> <p>XmlTextReader Reader = NULL;</p> <p>Reader = New XMLTextReader (stream);</p> <p>While (Reader.Read ())</p> <p>{</p> <p>Switch (Reader.NodeType)</p> <p>{</p> <p>Case XMLNodetype.Element: // The node is an element.</p> <p>Console.write ("<" Reader.Name);</p> <p>While (Reader.MovetonextAttRibute ()) // read attributes.</p> <p>Console.write ("" Reader.Name "= '" Reader.Value "');</p> <p>Console.write (">");</p> <p>Console.writeline (">");</p> <p>Break;</p> <p>Case XMLNodType.Text: // Display The Text in Each Element.</p> <p>Console.writeline (Reader.Value);</p> <p>Break;</p> <p>Case XMLNodetype.Endelement: // Display end of element.</p> <p>Console.write ("</" ready.name;</p> <p>Console.writeline (">");</p> <p>Break;</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p> <p>Generate and run your project.</p> <p>Back to top</p> <p>Refer to more information, see "XML IN Microsoft .NET: .NET Framework XML Classes and C # Offer Simple, Scalable Data Manipulation" (XML: .NET Framework XML class and C # provide simple, scalable) Data operation) One article, this article is</p> <p>In MSDN Magazine, you can access from the following Microsoft Web site:</p> <p>Http://msdn.microsoft.com/library/periodic/period01/xml0101.htm</p> <p>XmlReader,</p> <p>StreamReader and</p> <p>For more information on the StringReader class, see the Microsoft .NET Framework Class library document.</p> <p>Use</p> <p>For more information on XMLReader read XML data, see the Microsoft .NET Framework Developer's Guide document. Back to top</p> <p>The information in this article applies to:</p> <p>Microsoft .NET framework</p> <p>Recent Update: 2002-6-25 (1.0) Keyword Kbhowto KbhowTomaster KBXML TSLIC_TSLIC KB301228 KBAUDDEVELOPER</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-108985.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="108985" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.046</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'zlwETlOxemPCNWygDIjcx9E8_2Bde6Q3yTvSZ_2FpIoZJi3kB5ozLnJGIjGSaeqAIyUgqdlqzfRd4TPFRPZ85ocSaw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>