Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c# regular expression to parse MS Open XML fragment

Posted on 2012-04-04
5
Medium Priority
?
316 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

610 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