0

Look into this xml file:
<xml>
<head>
<title>Something</title>
</head>
</xml>

I need to store all the string from the tags into an array.
So my string array is supposed to be
[xml,head,title,/title,/head,/xml].

I've tried various methods and read up regular expressions tutorial for C#, but I can't get the string that I wanted.
Here is the code:

string ser = Regex.Replace(d[s], "[^<.*?>]", string.Empty);
MessageBox.Show(ser);

Any help will be appreciated thanks.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by jonsca
0

See if something like this will work for you:
(tags is a List<string>)

XmlReader xmlr = XmlReader.Create("Yourfilenamehere");
while (xmlr.Read())
{
  if (xmlr.NodeType == XmlNodeType.EndElement)
      tags.Add("/" + xmlr.Name);
  else
      tags.Add(xmlr.Name);
                
}

Then loop through and delete all the empty entries. After that use the string's replace method (I suppose you could use the RegEx one in one step) twice, once replacing "[" with "" and once replacing "]" with "".

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.