简体   繁体   English

了解xml结构的xml文件类型的最佳方法

[英]Best way to reckon a xml file type knowing xml structure

I want to read a xml file, and based on some xml structures I already know recognize the type of xml ( xml 1, xml 2 or xml 3 ) 我想读一个XML文件,并基于某些xml structures ,我已经知道识别XML的类型( xml 1, xml 2 or xml 3

for example, I know 3 xml structures: 例如,我知道3个xml结构:

xml 1 xml 1

<?xml version="1.0"?>
<OS>
  <Info>
   ...
  </Info>
</OS>

xml 2 XML 2

<?xml version="1.0" encoding="UTF-8"?>
<SaData Version="2">
  <Cli>
  ...
  </Cli>
</SaData>

xml 3 XML 3

<?xml version="1.0" encoding="UTF-8"?>
<Init>
     <Danit>
     ...
     </Danit>
</Init>

So far I have a class XMLBrandRecognitionNode that acts as a string enum for list of xmlTypes (I did it using a list because the number of xml types could grow, for now there are only 3 types) 到目前为止,我有一个XMLBrandRecognitionNode类,它充当XMLBrandRecognitionNode列表的string enum (我使用列表来完成此操作,因为xml类型的数量会增加,目前只有3种类型)

Public Class XMLBrandRecognitionNode
        Private Key As String
        Public Shared ReadOnly xml1 As XMLBrandRecognitionNode = New XMLBrandRecognitionNode("/OS/Info")
        Public Shared ReadOnly xml2 As XMLBrandRecognitionNode = New XMLBrandRecognitionNode("/SaData/Cli")
        Public Shared ReadOnly xml3 As XMLBrandRecognitionNode = New XMLBrandRecognitionNode("/Init/Danit")
        Private Sub New(ByVal key As String)
            Me.Key = key
        End Sub
        Public Overrides Function ToString() As String
            Return Me.Key
        End Function
    End Class

And then I populate the list as: 然后,我将列表填充为:

Dim recognitionList As New List(Of XMLBrandRecognitionNode)
recognitionList.Add(XMLTypeN.xml1)
recognitionList.Add(XMLTypeN.xml2)
recognitionList.Add(XMLTypeN.xml3)

Now the part to classify the file ( xml1,xml2,xml3 ) 现在是对文件进行分类的部分( xml1,xml2,xml3

Dim m_xmld As XmlDocument
m_xmld = New XmlDocument()
m_xmld.Load("myXML.xml")

However I do not know the best way to classify the type, I was thinking on doing a loop and based on the list of nodes that I get return the type 但是我不知道对类型进行分类的最佳方法,我在考虑做一个循环并根据得到的节点列表返回类型

For Each o As XMLBrandRecognitionNode In recognitionList
   Try
        child_nodes = m_xmld.GetElementsByTagName(o.ToString)
        'maybe a condition or something...

   Catch ex As Exception
         Continue For
   End Try
Next

What would be the correct way to reckon a xml file type? 估算xml文件类型的正确方法是什么?

There is XML Schema Definition , which is a standard way to describe the structure of your XML. XML Schema Definition ,这是描述XML结构的标准方法。 Create one XSD for each XML type and then you can test an XML document against each of your XSD to know which schema the XML conform to. 为每种XML类型创建一个XSD,然后可以针对每个XSD测试XML文档,以了解XML遵循的架构。

For simple scenario though, you might want to just write your own simple logic to classify your XML documents. 但是对于简单的情况,您可能只想编写自己的简单逻辑即可对XML文档进行分类。

This is one possible way. 这是一种可能的方式。 The method will return True and set the xmlType to the correct type if XML structure is recognized by one of the patterns , else the method simply return False : 如果其中一种patterns可以识别XML结构,则该方法将返回True并将xmlType设置为正确的类型,否则该方法仅返回False

Public Function CheckXmlType(doc As XmlDocument, ByRef xmlType As String) As Boolean
    xmlType = "not recognized"
    'you can change this to dictionary(of enum, string) if you like'
    Dim patterns = New Dictionary(Of String, String)()
    patterns.Add("type1", "/OS/Info")
    patterns.Add("type2", "/SaData/Cli")
    patterns.Add("type3", "/Init/Danit")
    For Each p In patterns
        Dim test = doc.SelectSingleNode(p.Value)
        If test IsNot Nothing
            xmlType = p.Key
            Return True
        End If
    Next

    Return False
End Function

And then you can use it like so : 然后您可以像这样使用它:

Dim m_xmld As XmlDocument
m_xmld = New XmlDocument()
m_xmld.Load("myXML.xml")

Dim xmlType As String = ""
If m_xmld.CheckXmlType(xmlType)
    'this should print type1/type2/type3 according to the content of myXML.xml'
    Console.WriteLine(xmlType)
Else
    Console.WriteLine("XML type is not recognized")
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM