如何解析XML?
这是我将得到的响应:
<?xml version="1.0" encoding="utf-8"?>
<rsp stat="ok">
<image_hash>cxmHM</image_hash>
<delete_hash>NNy6VNpiAA</delete_hash>
<original_image>http://imgur.com/cxmHM.png</original_image>
<large_thumbnail>http://imgur.com/cxmHMl.png</large_thumbnail>
<small_thumbnail>http://imgur.com/cxmHMl.png</small_thumbnail>
<imgur_page>http://imgur.com/cxmHM</imgur_page>
<delete_page>http://imgur.com/delete/NNy6VNpiAA</delete_page>
</rsp>
How can I extract the value of every tag?
XDocument response = new XDocument(w.UploadValues("http://imgur.com/api/upload.xml", values));
string originalImage = 'do the extraction here';
string imgurPage = 'the same';
UploadedImage image = new UploadedImage();
This is the response I'll be getting:
<?xml version="1.0" encoding="utf-8"?>
<rsp stat="ok">
<image_hash>cxmHM</image_hash>
<delete_hash>NNy6VNpiAA</delete_hash>
<original_image>http://imgur.com/cxmHM.png</original_image>
<large_thumbnail>http://imgur.com/cxmHMl.png</large_thumbnail>
<small_thumbnail>http://imgur.com/cxmHMl.png</small_thumbnail>
<imgur_page>http://imgur.com/cxmHM</imgur_page>
<delete_page>http://imgur.com/delete/NNy6VNpiAA</delete_page>
</rsp>
How can I extract the value of each tag?
XDocument response = new XDocument(w.UploadValues("http://imgur.com/api/upload.xml", values));
string originalImage = 'do the extraction here';
string imgurPage = 'the same';
UploadedImage image = new UploadedImage();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
幸运的是,这非常简单:
假设您的
XDocument
构造函数调用是正确的……在不知道w.UploadValues
做什么的情况下,这很难说。LINQ to XML 使查询变得非常简单 - 如果您有任何更复杂的问题,请告诉我们。
请注意,我使用了字符串转换,而不是采用
Value
属性或类似的内容。这意味着如果
元素丢失,originalImage
将为 null,而不是抛出异常。您可能更喜欢这种例外,具体取决于您的具体情况。Fortunately it's pretty simple:
That's assuming your
XDocument
constructor call is correct... without knowing whatw.UploadValues
does, that's hard to say.LINQ to XML makes querying very easy - let us know if you have anything more complicated.
Note that I've used a cast to string instead of taking the
Value
property or anything like that. That means if the<original_image>
element is missing,originalImage
will be null rather than an exception being thrown. You may prefer the exception, depending on your exact situation..NET 框架内置了一个优秀且易于使用的 XML 解析器。请参阅 此处供参考。
The .NET framework has an excellent, simple to use XML Parser built right in. See here for reference.
一种方法是使用 .net xsd.exe 工具< /a> 为您在问题中所述的 rsp xml 块创建一个包装类。创建类后,您可以简单地使用以下代码块将 xml 封装为可以直接在代码中使用的对象。当然,如果您愿意,总是有 Xpath 或 Jon 所说的 linq 作为选项将 xml 加载到 xmldocument 对象中,就像上面所做的那样。
享受!
One way is the use the .net xsd.exe tool to create a wrapper class for the rsp xml block you have stated in your question. Once you have the class created you can simply use the following code block to searealize the xml into an object you can use directly in your code. Of course there is always Xpath, or linq as Jon stated as options as well if you prefer to load the xml into and xmldocument object as you have done above.
Enjoy!