x Did you like it? Well, then please consider making a donation :)

XML file parsing with .NET Regex.Match

You can do this via regex or xpath, depending on the complexity of the xml. If you want to use regular expressions, you'd probably want to do something like this:

Tags: #file, #getting, #language, #match, #matches, #parsing, #regex.match, #struct, #structure, #text, #xml. Views: 3529. stackoverflow, 8/12/2015
Link 1: http://www.systemtextregularexpressions.com/m19/xml-file-parsing-with-.net-regex.match
Link 2: http://www.systemtextregularexpressions.com/m19 (short)

Length: 0b, Line: 1, Cursor: 0

RegexOptions

IsMatch=true


#ValueIndexLengthGroupsCaptures
0<?xml version="1.0"?>021Skip Navigation Links.
Expand Groups [6]Groups [6]
Skip Navigation Links.
Expand Captures [1]Captures [1]
1<text>226Skip Navigation Links.
Expand Groups [6]Groups [6]
Skip Navigation Links.
Expand Captures [1]Captures [1]
2<one>123</one>2914Skip Navigation Links.
Expand Groups [6]Groups [6]
Skip Navigation Links.
Expand Captures [1]Captures [1]
3<two params="3" />4418Skip Navigation Links.
Expand Groups [6]Groups [6]
Skip Navigation Links.
Expand Captures [1]Captures [1]
4<three params="1" p="2">value</three>6337Skip Navigation Links.
Expand Groups [6]Groups [6]
Skip Navigation Links.
Expand Captures [1]Captures [1]
5</text>1017Skip Navigation Links.
Expand Groups [6]Groups [6]
Skip Navigation Links.
Expand Captures [1]Captures [1]
Count: 6, Time: 00:00:00

Share (without adding to database):

Source code

0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
//.NET Regex.Match Online Tester: www.systemtextregularexpressions.com

//Namespace
using System.Text.RegularExpressions;

//Regex Pattern
string pattern = @"(<(?<name>.+?)>(.+?)</(?<name>.+?)>)|(<(?<name>.+?)(/|)>)";

//Regex example #1
Regex r = new Regex(@"(<(?<name>.+?)>(.+?)</(?<name>.+?)>)|(<(?<name>.+?)(/|)>)");
Match m1 = r.Match("input text");
MatchCollection mc1 = r.Matches("input text");

//Regex example #2
Match m2 = Regex.Match("input text", @"(<(?<name>.+?)>(.+?)</(?<name>.+?)>)|(<(?<name>.+?)(/|)>)");
MatchCollection mc2 = Regex.Matches("input text", @"(<(?<name>.+?)>(.+?)</(?<name>.+?)>)|(<(?<name>.+?)(/|)>)");
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
'.NET Regex.Match Online Tester: www.systemtextregularexpressions.com

'Namespace
Imports System.Text.RegularExpressions

'Regex Pattern
Dim pattern As String = "(<(?<name>.+?)>(.+?)</(?<name>.+?)>)|(<(?<name>.+?)(/|)>)"

'Regex example #1
Dim r As Regex = New Regex("(<(?<name>.+?)>(.+?)</(?<name>.+?)>)|(<(?<name>.+?)(/|)>)")
Dim m1 As Match = r.Match("input text")
Dim mc As MatchCollection = r.Matches("input text")

'Regex example #2
Dim m2 As Match = Regex.Match("input text", "(<(?<name>.+?)>(.+?)</(?<name>.+?)>)|(<(?<name>.+?)(/|)>)")
Dim mc2 As MatchCollection = Regex.Matches("input text", "(<(?<name>.+?)>(.+?)</(?<name>.+?)>)|(<(?<name>.+?)(/|)>)")

Home     Match     Replace     Split     Help     Library     Contacts     netregex rss

Made by Koshevoy Dmitry [8Bytes],

© 2014-2024 Ukraine, Nikolaev.