307548 How to: Use Visual C # .NET to read XML (from mkba) from the file

xiaoxiao2021-03-06  74

This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.

For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.

For Microsoft Visual Basic .NET versions of this article, see

CHS301225.

This task content

Summary

Require how to read XML data complete code list example output reference

Summary How to use this article

The XMLTextReader class reads the Scalable Markup Language (XML) from the file.

XMLTextReader can perform syntax analysis and tags of XML, and implement XML 1.0 specification and namespaces in the WWW Federation (W3C) XML specification. This article provides fast, marking stream access to XML, rather than using an object model such as an XML document object model (DOM).

Back to top

The following table summarizes the recommended hardware, software, network architecture, and service pack required:

Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:

XML terminology creates and read XML files

Back to top

How to read the XML this example from a file Using a file called Books.xml. You can create your own books.xml file, or use the .NET Software Development Kit (SDK) to quickly get started with the sample file included in the following folder.

/ Program Files / Microsoft Visual Studio .NET / FRAMEWORKSDK / Samples / QuickStart / HOWTO / SAMPLES / XML / TransformXML / CS must copy the books.xml file to the / bin / debug folder, which is located in the file where the project is created. The lower side of the clip. You can also download books.xml; for download, see

Reference section.

Open Visual Studio .NET. New Visual C # .NET Console Application. You can read the full code list directly, or continue to perform the following steps to generate an application. Make sure the project contains references to System.xml.dll assembly. Specify Using instructions on the System.xml namespace so that you don't need to define XMLTextReader declaration in your code. The USING instruction must be before any other declaration. Using System.xml; Create an instance of an XMLTextReader object and populates the instance using the XML file. Typically, if you need to access XML as raw data without producing a DOM overhead, use an XMLTextReader class; therefore, the XMLTextReader class provides a more quickly read XML mechanism. The XMLTextReader class uses a different constructor to specify the location of the XML data. The following code creates an instance of an XMLTextReader object and loads the books.xml file. Add the following code to the main process of the Class1: XMLTextReader Reader = New XMLTextReader ("Books.xml"); read all XML data. (Note that this step demonstrates an external "While" loop and demonstrates how to use this loop to read XML.) After creating the XMLTextReader object, use the Read method to read XML data. The READ method continues sequentially read the XML file until the end of the file is reached, at which time the READ method returns "false" value. While (Reader.Read ()) {

// Do some work here on the data.

Console.writeLine (Reader.Name);

}

Console.readline (); check node. To process XML data, each record should have a node type that can be determined by NodetyPE properties. 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 code example shows the name and document type of the element. Note that this example ignores the element properties. While (Reader.Read ())

{

Switch (Reader.NodeType)

{

Case XMLNodetype.Element: // The node is an element.

Console.write ("<" Reader.Name);

Console.writeline (">");

Break;

Case XMLNodType.Text: // Display The Text in Each Element.

Console.writeline (Reader.Value);

Break;

Case XMLNodType. endElement: // display the end of the element.

Console.write ("

Console.writeline (">");

Break;

}

} 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. While (Reader.Read ())

{

Switch (Reader.NodeType)

{

Case XMLNodetype.Element: // The node is an element.

Console.write ("<" Reader.Name);

While (Reader.MovetonextAttribute ()) // read the attributes.

Console.write ("" Reader.Name "= '" Reader.Value "');

Console.write (">");

Console.writeline (">");

Break;

Case XMLNodType.Text: // Display The Text in Each Element.

Console.writeline (Reader.Value);

Break;

Case XMLNodType. endElement: // display the end of the element.

Console.write ("

Console.writeline (">");

Break;

}

} Save and shut down the project.

Back to top

Complete code list

Using system;

USING SYSTEM.XML;

Namespace ReadXmlFromfile

{

///

/// summary description for class1.

///

Class class1

{

Static void main (string [] args)

{

XMLTextReader Reader = New XMLTextReader ("Books.xml");

While (Reader.Read ())

{

Switch (Reader.NodeType)

{

Case XMLNodetype.Element: // The node is an element.

Console.write ("<" Reader.Name);

Console.writeline (">");

Break;

Case XMLNodType.Text: // Display The Text in Each Element.

Console.writeline (Reader.Value);

Break;

Case XMLNodetype.Endelement: // Display the end of the element.

Console.write ("

Console.writeline (">");

Break;

}

}

Console.readline ();

}

}

}

Back to top

Example output

</p> <p>The Autobiography of Benjamin Franklin</p> <p></ title></p> <p><author></p> <p><first-name></p> <p>Benjamin</p> <p></ first-name></p> <p><Last-Name></p> <p>Franklin</p> <p></ Last-Name></p> <p></ author></p> <p><price></p> <p>8.99</p> <p></ price></p> <p></ book></p> <p><book> <title></p> <p>The Confidence Man</p> <p></ title></p> <p><author></p> <p><first-name></p> <p>Herman</p> <p></ first-name></p> <p><Last-Name></p> <p>Melville</p> <p></ Last-Name></p> <p></ author></p> <p><price></p> <p>11.99</p> <p></ price></p> <p></ book></p> <p><book></p> <p><Title></p> <p>The gorgias</p> <p></ title></p> <p><author></p> <p><Name></p> <p>Plato</p> <p></ name></p> <p></ author></p> <p><price></p> <p>9.99</p> <p></ price></p> <p></ book></p> <p></ bookstore></p> <p>Back to top</p> <p>Refer to the following files to download from the Microsoft Download Center:</p> <p>Download Books.xml now For more information, see "XML IN .NET: .NET Framework XML Classes and C # Offer Simple, Scalable Data Manipulation" (. NET XML: .NET Framework XML class and C # provide simple, Zoom 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/default.asp?url=/library/en-us/dnmag01/html/xml0101.asp</p> <p>For more information on the XMLReader class, please visit the following .NET Framework Class Library Web Site:</p> <p>http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemxmlxmlreadERCLASTOPIC.HTM for how to use</p> <p>XmlReader reads more information about XML data, see the Microsoft .NET Framework Developer Document Guide documentation below:</p> <p>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconreadingXMLDataUsingXMLReader.asp</p> <p>Back to top</p> <p>The information in this article applies to:</p> <p>Microsoft Visual C # .NET Beta 2</p> <p>Recent Update: 2004-8-13 (1.2) Keywords: KBDownload Kbhowto KbhowTomaster KB307548 KBAUDDEVELOPER</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-108930.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="108930" 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.047</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 = 'XJSP6OGMnasS2_2FmK5lNlTC5mfLgtHoy3jxHBcTOXYYmOIwaBOmpjo5676_2B1QtCCl6gmgcFa2O2EVdSO9VWGneA_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>