Alternatively you can use an XML parser like so:
string xml = "<Canvas Background=\"#FF00FFFF\" Name=\"Page_1\" Width=\"1200\" Height=\"900\" ><TextBlock Name=\"PageTitle\" /></Canvas><Canvas Background=\"#FF00FFFF\" Name=\"Page_2\" Width=\"1200\" Height=\"900\"><TextBlock Name=\"PageTitle\" /></Canvas>";
xml = "<Outer>" + xml + "</Outer>";
XDocument doc = XDocument.Parse(xml);
string[] array = doc.Descendants("Canvas").Select(item => item.ToString(SaveOptions.DisableFormatting)).ToArray();
Then array[]
will contain what you want. This might be more generally useful.
This gives you the opportunity to do some higher-level parsing like so (Console App code):
string xml = "<Canvas Background=\"#FF00FFFF\" Name=\"Page_1\" Width=\"1200\" Height=\"900\" ><TextBlock Name=\"PageTitle1\" /></Canvas><Canvas Background=\"#FF00FFFF\" Name=\"Page_2\" Width=\"1200\" Height=\"900\"><TextBlock Name=\"PageTitle2\" /></Canvas>";
xml = "<Outer>" + xml + "</Outer>";
XDocument doc = XDocument.Parse(xml);
var items = from item in doc.Descendants("Canvas") select new
{
Background = (string) item.Attribute("Background"),
Name = (string) item.Attribute("Name"),
Width = (int) item.Attribute("Width"),
Height = (int) item.Attribute("Height"),
TextBlockName = (string) item.Element("TextBlock").Attribute("Name")
};
foreach (var item in items)
{
Console.WriteLine
(
"Background = {0}\n" +
"Name = {1}\n" +
"Width = {2}\n" +
"Height = {3}\n" +
"TextBlockName = {4}\n",
item.Background,
item.Name,
item.Width,
item.Height,
item.TextBlockName
);
}
The output from this code is:
Background = #FF00FFFF
Name = Page_1
Width = 1200
Height = 900
TextBlockName = PageTitle1
Background = #FF00FFFF
Name = Page_2
Width = 1200
Height = 900
TextBlockName = PageTitle2