2013-08-23 4 views
-1

J'essaie d'extraire quelques informations d'un fichier XML généré par une autre application, c'est une version légèrement réduite pour plus de clarté.Analyse XML dans VB .Net

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<!--IE2C Current Input--> 
<RSLogix5000Content SchemaRevision="1.0" SoftwareRevision="19.01" > 
<Controller Use="Context" Name="MAIN"> 
<AddOnInstructionDefinitions Use="Context"> 
<EncodedData EncryptionConfig="2"> 
<Description> 
<![CDATA[IE2C Current Input]]> 
</Description> 
<RevisionNote> 
<![CDATA[Initial Revision]]> 
</RevisionNote> 
zNti6YvCK0McoTr4NZK1iyGdEAkM0sxvRC35nnfg/Gd6x+f1lAbsW0PwA4f9TfqHs3MmOQ9OhS9...</EncodedData> 
</AddOnInstructionDefinitions> 
</Controller> 
</RSLogix5000Content> 

Je peux retourner avec succès la valeur EncryptionConfig (2) en utilisant le code suivant:

Dim xml As XPathDocument = New XPathDocument(filepath) 
xmlNav = xml.CreateNavigator() 
xmlNI = xmlNav.Select("//EncodedData") 
While (xmlNI.MoveNext()) 
    If IsNumeric(xmlNI.Current.GetAttribute("EncryptionConfig", "")) Then 
     encryptionconfig = xmlNI.Current.GetAttribute("EncryptionConfig", "") 
    Else 
     encryptionconfig = 0 
    End If 
End While 
xmlNI = Nothing 
xmlNav = Nothing 
xml = Nothing 

Le problème est que je dois aussi revenir juste la valeur 'zNti6YvCK0McoTr4NZK1iyGdEAkM0 ...' aussi. Tout ce que j'ai essayé jusqu'ici le renvoie avec les valeurs Description et RevisionNote dont je ne veux pas.

Mise à jour

Ceci est le code final permettant des fichiers non codés et certaines versions de fichiers qui ne contiennent pas l'attribut EncryptionConfig:

Dim b64text As String = "" 
Dim encryptionconfig As Integer = 0 
Dim xml As XElement = XElement.Load(filepath) 
Dim node = xml.Descendants("EncodedData")(0) 
If node IsNot Nothing Then 
    b64text = node.Nodes().OfType(Of XText)().First().Value.Trim() 
    If node.Attribute("EncryptionConfig") IsNot Nothing Then 
     encryptionconfig = node.Attribute("EncryptionConfig").Value 
    End If 
Else 
    Label6.ForeColor = Color.Red 
    Label6.Text = "File is not encoded" 
End If 
node = Nothing 
xml = Nothing 
+0

Est-ce XML valide? La partie 'zNti6YvC ...' me semble bizarre. Ce n'est pas un attribut ni un enfant? –

+0

Il s'agit en fait d'un code XML codé en base64 que l'application ajoute au code XML principal. Je suppose à cet égard que ce n'est pas valable, ce qui explique probablement pourquoi j'ai de la difficulté à extraire cette chaîne car elle n'a pas de nom d'attribut. Ce que je suis en train de faire, c'est d'extraire la chaîne base64, de la déchiffrer et d'insérer le XML résultant dans le fichier XML principal. –

Répondre

1

Vous devez vérifier le type de tous les nœuds <EncodedData> pour le texte. Une façon simple serait d'utiliser linq2xml, la méthode OfType et le type XText:

Exemple:

Dim xml = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
      <!--IE2C Current Input--> 
      <RSLogix5000Content SchemaRevision="1.0" SoftwareRevision="19.01" > 
      <Controller Use="Context" Name="MAIN"> 
      <AddOnInstructionDefinitions Use="Context"> 
      <EncodedData EncryptionConfig="2"> 
      <Description> 
      <![CDATA[IE2C Current Input]]> 
      </Description> 
      <RevisionNote> 
      <![CDATA[Initial Revision]]> 
      </RevisionNote> 
      zNti6YvCK0McoTr4NZK1iyGdEAkM0sxvRC35nnfg/Gd6x+f1lAbsW0PwA4f9TfqHs3MmOQ9OhS9...</EncodedData> 
      </AddOnInstructionDefinitions> 
      </Controller> 
      </RSLogix5000Content> 

Dim node = xml.Descendants("EncodedData")(0) 
Dim data = node.Nodes().OfType(Of XText)().First().Value.Trim() ' is zNti6YvCK0McoTr4NZK1iyGdEAkM0sxvRC35nnfg/Gd6x+f1lAbsW0PwA4f9TfqHs3MmOQ9OhS9... ' 
Dim config = node.Attribute("EncryptionConfig").Value ' is 2 '