0

Can someone help me?

My code:

    Private Sub setDeny(ByVal text As String)
    Dim pattern As String = "(?<=<DeniedWebsiteList>)(.*)(?=</DeniedWebsiteList>)"
    Dim m1 As MatchCollection = Regex.Matches(text, pattern)
    pattern = "(?<=<Website>)(.*)(?=</Website>)"
    Dim m2 As MatchCollection = Regex.Matches(m1(0).ToString, pattern)

    For Each website In m2
        deniedWebsiteList.Add(website.ToString)
    Next
End Sub

The text value is this xml file:

<?xml version="1.0" ?>
<Config>

  <DeniedWebsiteList>
    <Website>
      sapo.pt
    </Website>
    <Website>
      facebook.com
    </Website>
  </DeniedWebsiteList>
</Config>

When I run the code Doesn't find any pattern on the text.

ABelo
  • 31
  • 6
  • 6
    I'm not going to answer this question. The only thing I have to say is: never, **never** use regular expressions on an XML file when you have a proper XML parser at hand - you will most probably get it wrong. As .NET does have a proper XML parser, use that to parse your XML. – Thorsten Dittmar Oct 18 '16 at 09:11
  • @ThorstenDittmar Thanks for the hint, it was what I needed – ABelo Oct 18 '16 at 09:12
  • @ABelo HTML is often not valid XML however, so something like the HtmlAgilityPak is often recommended for parsing HTML, as it is generally more tolerant than a strict XML parser. It's basically a fault-tolerant XML parser. – RB. Oct 18 '16 at 09:44
  • Have a look at [MS' Examples Page](https://msdn.microsoft.com/en-us/library/58a18dwa(v=vs.110).aspx) on XML Serialization – Clijsters Oct 18 '16 at 11:51
  • [**Why you shouldn't parse XML/HTML with Regex**](http://stackoverflow.com/a/1732454/3740093). – Visual Vincent Oct 18 '16 at 21:18

0 Answers0