?
Solved

c# regular expression to parse MS Open XML fragment

Posted on 2012-04-04
5
Medium Priority
?
314 Views
Last Modified: 2012-06-27
I am looking for an accurate way to parse the following, I am assuming regex or maybe XML parser. This is for a c# application.

examples:

"w:name w:val=\"CALC_OFFICEADDRFULL\"
"w:enabled w:val=\"true\"
"w:calcOnExit w:val=\"false\"
"w:type w:val=\"regular\"

expected result (key value or something)

name,CALC_OFFICEADDRFULL
enabled,true
calcOnExit,false
type,regular

Much appreciated,

-Markus
0
Comment
Question by:markusr13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37805958
Please try the following:
var rxKey = new Regex(@"(?<=w:)\w+(?=\s)");
var rxValue = new Regex("(?<=w:val=\\\\\").+(?=\\\\\")");
var tests = new string[]
{
	"\"w:name w:val=\\\"CALC_OFFICEADDRFULL\\\"",
	"\"w:enabled w:val=\\\"true\\\"",
	"\"w:calcOnExit w:val=\\\"false\\\"",
	"\"w:type w:val=\\\"regular\\\""
};

foreach (var test in tests)
{
	Console.WriteLine("{0},{1}", rxKey.Match(test).Value, rxValue.Match(test).Value);
}

Open in new window

Output:
name,CALC_OFFICEADDRFULL
enabled,true
calcOnExit,false
type,regular

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37806007
Here is another (faster) option with simple substrings:
var tests = new string[]
{
	"\"w:name w:val=\\\"CALC_OFFICEADDRFULL\\\"",
	"\"w:enabled w:val=\\\"true\\\"",
	"\"w:calcOnExit w:val=\\\"false\\\"",
	"\"w:type w:val=\\\"regular\\\""
};

foreach (var test in tests)
{
	var key = test.Substring(3, test.IndexOf(" ") - 3);

	int i = test.IndexOf("\\\"") + 2;
	var value = test.Substring(i, test.Length - i - 2);
	
	Console.WriteLine("{0},{1}", key, value);
}

Open in new window

0
 

Author Comment

by:markusr13
ID: 37807754
Sorry,

The debugger through in the \'s (and i had an extra quote)

try

w:name w:val="CALC_OFFICEADDRFULL"
w:enabled w:val="true"
w:calcOnExit w:val="false"
w:type w:val="regular"

-Markus
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 37807787
Not a problem.  There you go.

Option 1: (Regex)
var rxKey = new Regex(@"(?<=w:)\w+(?=\s)");
var rxValue = new Regex("(?<=w:val=\").+(?=\")");

var tests = new string[]
{
"w:name w:val=\"CALC_OFFICEADDRFULL\"",
"w:enabled w:val=\"true\"",
"w:calcOnExit w:val=\"false\"",
"w:type w:val=\"regular\""
};

foreach (var test in tests)
{
    Console.WriteLine("{0},{1}", rxKey.Match(test).Value, rxValue.Match(test).Value);
}

Open in new window


Option 2: (Substring)
var tests = new string[]
{
"w:name w:val=\"CALC_OFFICEADDRFULL\"",
"w:enabled w:val=\"true\"",
"w:calcOnExit w:val=\"false\"",
"w:type w:val=\"regular\""
};

foreach (var test in tests)
{
	var key = test.Substring(2, test.IndexOf(" ") - 2);

	int i = test.IndexOf("\"") + 1;
	var value = test.Substring(i, test.Length - i - 1);
	
	Console.WriteLine("{0},{1}", key, value);
}

Open in new window

0
 

Author Comment

by:markusr13
ID: 37807789
points increased due to my data error.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question