Problème avec XML particulier
Bonjour a tous, voila j'ai sur un mon programme besoin de lire des fichiers XML dont la structure est fixe mais particulière
Code:
1 2 3 4 5 6 7
|
<test>
<sec1>
<info name="test" url="http://www.developpez.net" />
<info2> d: </info2>
</sec1>
</test> |
Dans le cas d'une structure simple " <info2> d: </info2> " aucun problème pour lire l'information mais dans le cas de
la première ligne je ne trouve pas comment le lire d'autant que j'ai plusieurs information a lire ...
Code:
1 2 3 4 5
|
Dim MonXML As XDocument = XDocument.Load("E:\test.xml")
MsgBox(MonXML.<test>.<sec1>.<info2>.Value())
MsgBox(MonXML.<test>.<sec1>.<info1>.Value()) |
Merci d'avance pour votre aide
Bye
Tout fonctionne a merveille
Voila mon code source qui marche parfaitement mais si vous voyez des optimisation a faire :D
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| ' Définitions des variables
Dim Path As String
Dim ID As String
Dim Result As Integer
Dim SplitLine As String()
' Ouverture du premier fichier
Dim containerXML As XDocument = XDocument.Load(TempDir & "META-INF\container.xml")
Dim CX As XNamespace = containerXML.Root.Name.Namespace
' Récupération du lien du second fichier avec remplacement des / par des \ ( pour Windows )
Path = Replace(containerXML.Descendants(CX + "rootfile").FirstOrDefault.Attribute("full-path").Value, "/", "\")
' Ouverture du second fichier
Dim ContentOPF As XDocument = XDocument.Load(TempDir & Path)
Dim CO As XNamespace = ContentOPF.Root.Name.Namespace
'Lecture des informations sur le livre et ecriture dans les variables
Ebook.Titre = ContentOPF.Descendants(CO + "metadata").Elements(GetXmlNamespace(dc) + "title").Value
Ebook.Auteur = ContentOPF.Descendants(CO + "metadata").Elements(GetXmlNamespace(dc) + "creator").Value
Ebook.Editeur = ContentOPF.Descendants(CO + "metadata").Elements(GetXmlNamespace(dc) + "publisher").Value
Ebook.DatePubliction = ContentOPF.Descendants(CO + "metadata").Elements(GetXmlNamespace(dc) + "date").Value
Ebook.Langue = ContentOPF.Descendants(CO + "metadata").Elements(GetXmlNamespace(dc) + "language").Value
Ebook.Description = ContentOPF.Descendants(CO + "metadata").Elements(GetXmlNamespace(dc) + "description").Value
' On cherche les 2 ID
Dim Allidentifier = From xElem In ContentOPF.Descendants(CO + "metadata").Elements(GetXmlNamespace(dc) + "identifier") Select xElem
For Each Identifier In Allidentifier
ID = Identifier.Attribute(GetXmlNamespace(opf) + "scheme")
If ID = "ISBN" Then
Ebook.ISBN = Identifier.Value
ElseIf ID = "uuid" Then
Ebook.UUID = Identifier.Value
End If
Next
Result = InStrRev(Path, "\") 'On recherche le dernier \
If Result Then Path = Mid(Path, 1, Result) Else Path = ""
' Si on trouve on coupe la variable ( on enleve juste le nom du premier fichier et on recupere le(s) dossier(s)
' On lance la recherche de l'image de couverture du livre ( un vrai bordel ^^ )
Dim AllManifestItem = From xElem In ContentOPF.Descendants(CO + "manifest").Elements(CO + "item") Select xElem
For Each Item In AllManifestItem
If Item.Attribute("id") = "cover" And Item.Attribute("media-type") = "image/jpeg" Then ' Si l'id = corver et le type de fichier = image = Jackpot
Ebook.CouverturePath = Path & Replace(Item.Attribute("href").Value, "/", "\") ' On recupere le chemin de l'image
Exit For ' et on quitte la boucle
ElseIf Item.Attribute("id") = "cover" And Item.Attribute("media-type") = "application/xhtml+xml" Then
Dim Html As String() = File.ReadAllLines(TempDir & Path & Item.Attribute("href").Value) ' Ouverture du fichier html
For Each Line In Html ' pour chaque ligne
If InStr(Line, "<img") Then ' si elle contient <img
SplitLine = Split(Line, """") 'on la coupe par les "
For boucle = 0 To SplitLine.Length - 1 'debut de la boucle
If InStr(SplitLine(boucle), "src=") Then ' si la ligne contient "Str"
Ebook.CouverturePath = Path & SplitLine(boucle + 1) ' on recupere le nom du fichier est remplie la variable
Exit For
End If
Next
Exit For
End If
Next
Exit For
ElseIf Item.Attribute("media-type") = "image/jpeg" Then
If IsNothing(Ebook.CouverturePath) Then Ebook.CouverturePath = Path & Replace(Item.Attribute("href").Value, "/", "\")
If InStr(Item.Attribute("id"), "cover") <> 0 Then
Ebook.CouverturePath = Path & Replace(Item.Attribute("href").Value, "/", "\")
Exit For
End If
End If
Next |
D'avance merci a tous et encore merci pour l'aide que vous m'avez apporter