如何使用 VB.NET 反序列化 5-6 级标签 [英] How to deserialize 5-6 level tags using VB.NET

查看:28
本文介绍了如何使用 VB.NET 反序列化 5-6 级标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<亚太地区><sasia><印度><印度><tn>钦奈<tn>马杜赖<亚太地区><sasia><印度><印度><ka>班加罗尔<ka>迈索尔

当我尝试使用 4 级标签时,它可以工作,但对于 5 级标签则无济于事.

我试过:尝试使用 VB 序列化和反序列化 xml 文件.net

解决方案

假设你的 xml 有正确的关闭标签等,

这里有一些你可以用来定义模型的类

公开课世界<XmlElement("apac")>公共财产 Apacs As List(Of Apac)结束类亚太公开课<XmlElement("sasia")>公共财产 Sasia As Sasia结束类公开课 Sasia<XmlElement("印度")>公共财产印度作为印度结束类印度公开课<XmlElement("sindia")>公共财产 Sindia 作为 Sindia结束类公共课辛迪亚<XmlElement("ka")>公共属性 Kas As List(Of String)<XmlElement("tn")>公共属性 Tns 作为列表(字符串)结束类

和可用于反序列化的代码

将我的世界变暗为世界Dim s As New XmlSerializer(GetType(World))使用 sr 作为新的 StreamReader("filename.xml")myWorld = DirectCast(s.Deserialize(sr), World)结束使用

以及可以用来测试的代码.

Console.WriteLine(myWorld.Apacs.First().Sasia.India.Sindia.Tns.First())Console.WriteLine(myWorld.Apacs.Last().Sasia.India.Sindia.Kas.First())

<块引用>

钦奈
班加罗尔

当然,如果你的 xml 看起来不是这样(我必须承认我做了一些猜测),那么这段代码将不起作用.如果必须,请在需要时关闭 xml 标记来更新您的问题,我们可以制定正确的解决方案.

<world> 
    <apac>
        <sasia> 
            <india>
                <sindia>
                    <tn>CHENNAI
                    <tn>Madurai





    <apac>
        <sasia> 
            <india>
                <sindia>
                    <ka>Bangalore
                    <ka>Mysore

When I try 4 level tag its works, but for 5 level tag it doesn't help.

I've tried: trying to serialize and deserialize an xml file using VB.net

解决方案

Assuming your xml has properly closed tags etc.,

<?xml version="1.0" encoding="utf-8" ?>
<world>
  <apac>
    <sasia>
      <india>
        <sindia>
          <tn>CHENNAI</tn>
          <tn>Madurai</tn>
        </sindia>
      </india>
    </sasia>
  </apac>
  <apac>
    <sasia>
      <india>
        <sindia>
          <ka>Bangalore</ka>
          <ka>Mysore</ka>
        </sindia>
      </india>
    </sasia>
  </apac>
</world>

here are some classes you can use to define your model

<XmlRoot("world")>
Public Class World
    <XmlElement("apac")>
    Public Property Apacs As List(Of Apac)
End Class
Public Class Apac
    <XmlElement("sasia")>
    Public Property Sasia As Sasia
End Class
Public Class Sasia
    <XmlElement("india")>
    Public Property India As India
End Class
Public Class India
    <XmlElement("sindia")>
    Public Property Sindia As Sindia
End Class
Public Class Sindia
    <XmlElement("ka")>
    Public Property Kas As List(Of String)
    <XmlElement("tn")>
    Public Property Tns As List(Of String)
End Class

and code you can use to deserialize

Dim myWorld As World
Dim s As New XmlSerializer(GetType(World))
Using sr As New StreamReader("filename.xml")
    myWorld = DirectCast(s.Deserialize(sr), World)
End Using

and code you can test it with.

Console.WriteLine(myWorld.Apacs.First().Sasia.India.Sindia.Tns.First())
Console.WriteLine(myWorld.Apacs.Last().Sasia.India.Sindia.Kas.First())

CHENNAI
Bangalore

Of course, if you xml doesn't look like this (I must admit I did some guesswork), then this code won't work. If you must, update your question by closing xml tags when required and we can work on the right solution.

这篇关于如何使用 VB.NET 反序列化 5-6 级标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆