307643 HOW TO: Use Visual C # .NET to read XML data from the URL (from mkba)

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 getting support for Beta versions, see the documentation included in the beta product file, or view the site you download this version.

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

CHS301232.

This task content

summary

Require how to read XML data from URL Sample Output Reference

Summary How to use this article

The XMLTextReader class reads the extended tag language (XML) from the Unified Resource Locator (URL). Flow information can have a variety of sources, such as from servers, files or

The byte stream of the TextReader class.

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 reads XML URL and creates an XML endpoint

Back to top

How to read an XML data from a URL This example uses a file named books.xml. You can create your own books.xml file or use the sample file included in the .NET Software Development Kit (SDK). You can also download this file; see this article for information on downloading location.

The first article of the reference section.

Copy the books.xml file to the / Inetpub / wwwroot folder on your computer. Open Visual Studio .NET. New Visual C # .NET Console Application. You can go to the full code list section, or you can continue to perform these steps to generate an application. Specify Using instructions on the System.xml namespace so that you don't need to define the XMLTextReader class in the code. The USING instruction must be before any other declaration. Using system.xml; retrieve XML stream via URL. The flow is used to provide independence between the devices, so if the source of the stream changes, the procedure is not required to change. Declare a constant to http: //localhost/books.xml URL. In the next step, this constant is used for XMLTextReader. Add the following code example to this default class MAIN process: string urlstring = "http://localhost/books.xml"; create an instance of the XMLTextReader class and specify the URL. Typically, use XMLTextReader if you need to use XML as raw data without generating a document object model (DOM) overhead; therefore, XMLTextReader provides a mechanism for reading XML faster. The XMLTextReader class uses a different constructor to specify the location of the XML data. The following code creates an instance of the XMLTextReader object and passes the URL to the constructor: XMLTextReader Reader = New XMLTextReader (UrlString); read all XML data. (Note that this step shows a basic external "While" loop, how to use this loop and read XML.) After loading, XMLTextReader will continue to read XML data continuously and use the READ method to get the next record. If there is no record, the read method will return false. While (Reader.Read ())

{

// do some work here on the data.console.writeline (ready.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;

}

} Generate and run your project.

Back to top

Complete code list

Using system;

USING SYSTEM.XML;

Namespace READXMLFROMURL

{

///

/// summary description for class1.

///

Class class1 {

Static void main (string [] args)

{

String urlstring = "http://localhost/books.xml";

XmlTextReader Reader = New XMLTextReader (Urlstring);

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;

}

}

}

}

}

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 genre = 'novel' publicationdate = '1967' isbn = '0-201-63361-2' >></p> <p><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 genre = 'philosophy' publicationDate = '1991' isbn = '1-861001-57-6' >> <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, see the following Microsoft .NET Framework Class library documentation:</p> <p>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemXMLXMLREADERCLASSTOPIC.ASP for how to use</p> <p>XmlReader reads more information about XML data, see the Microsoft .NET Framework Developer's Guide Document:</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 KB307643 KBAUDDEVELOPER</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-108903.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="108903" 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.050</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 = 'yo82LMffLEALHVL6MapcJelGZcmUC3Gy4G33UyX1zgqhlWYKSNCI76OmqR_2BfG7U13LBdC977qCwx4uFPpIGAxg_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>