【问题标题】:how to parse attribute out of this xml?如何解析这个xml的属性?
【发布时间】:2013-11-03 23:19:02
【问题描述】:

我一直试图从这个 xml 中找出宽度和高度:

<?xml version="1.0" encoding="utf-8"?>
<Collection MaxLevel="7" TileSize="256" Format="png" NextItemId="1" ServerFormat="Default" xmlns="http://schemas.microsoft.com/deepzoom/2009">
  <Items>
    <I Id="0" N="0" Source="157_images/16.XML">
      <Size Width="1200" Height="900" />
      <Viewport Width="1" X="0" Y="0" />
    </I>
  </Items>
</Collection>

但这是不可能的,xml 本身看起来不像我使用的其他 xml 文件 我到目前为止的代码是:

    path = "157.xml"
    Dim document = XDocument.Load(Server.MapPath(path))
    Dim Xml As String = document.ToString
    Dim myallelements As XElement = XElement.Parse(Xml)

【问题讨论】:

    标签: xml vb.net xml-parsing xelement


    【解决方案1】:

    这种笨拙的语法包含了默认命名空间:

     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim path As String = "C:\test\157.xml"
        Dim n As XmlNode
        Dim xm As New XmlDocument
    
        Dim nsmgr As New XmlNamespaceManager(xm.NameTable)
        nsmgr.AddNamespace("q", "http://schemas.microsoft.com/deepzoom/2009")
    
        xm.Load(path)
        n = xm.SelectSingleNode("/q:Collection/q:Items/q:I/q:Size", nsmgr)
    
        Debug.Print("width = " & n.Attributes("Width").Value & "  height = " & n.Attributes("Height").Value)
    
    End Sub
    

    【讨论】:

      【解决方案2】:
       Dim size = myallelements...<Collection>...<Items>...<I>...<Size>
       Dim width = size.@Width.Value
       Dim Height = size.@Height
      

      【讨论】:

        猜你喜欢
        • 2012-06-08
        • 2014-07-13
        • 1970-01-01
        • 2013-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多