Solved

XML Analysis using C# and Regex

Posted on 2008-10-22
3
329 Views
Last Modified: 2012-08-14
Hi

I would appreciate help with all or any of the numbered issues listed below. Thanks!!

Given:
* The XML Template String in Snippet A
* The Incoming XML String in Snippets B (exact structure as A)
* The Incoming XML String in Snippets C (same structure as A, but with missing element (price))
* The Incoming XML String in Snippets D (totally different structure to A)

I need to be able to determine the following about the xml String structures:

1) Does Snippet B conform COMPLETELY to the same structure as Snippet A (true / false)
If true, compare A to B and create a dictionary of the variableName / Values based on the two, e.g.

[genre][autobiography]
[title][The Autobiography of Benjamin Franklin]
[firstName][Benjamin]
[lastName][Franklin]
[price][8.99]

2) Does Snippet C conform COMPLETELY to the same structure as Snippet A (true / false)
If false,  Does Snippet C conform PARTIALLY to the same structure as Snippet A (true / false)
If true, Compare A  to C and create a dictionary of the variableName / Values based on the two, e.g.

[genre][autobiography]
[title][The Autobiography of Benjamin Franklin]
[firstName][Benjamin]
[lastName][Franklin]

3) Does Snippet D conform COMPLETELY to the same structure as Snippet A (true / false)
(Will always be false)

If somehow you can show me how to make these determinations I would be truly grateful.
Thanks

// SNIPPET A
String xmlTemplate="
<bookstore>
     <book genre="{genre}">
       <title>{title}</title>
       <author>
         <first-name>{firstName}</first-name>
         <last-name>{lastName}</last-name>
       </author>
       <price>{price}</price>
     </book>
</bookstore>
";
 
// SNIPPET B
String xmlIncoming="
<bookstore>
     <book genre="autobiography">
       <title>The Autobiography of Benjamin Franklin</title>
       <author>
         <first-name>Benjamin</first-name>
         <last-name>Franklin</last-name>
       </author>
       <price>8.99</price>
     </book>
</bookstore>";
 
// SNIPPET C
String xmlIncoming="
<bookstore>
     <book genre="autobiography">
       <title>The Autobiography of Benjamin Franklin</title>
       <author>
         <first-name>Benjamin</first-name>
         <last-name>Franklin</last-name>
       </author>
     </book>
</bookstore>";
 
// SNIPPET D
String xmlIncoming="
<bookstore>
     <book frontCover="true">
      <author>
         <first-name>Benjamin</first-name>
         <last-name>Franklin</last-name>
       </author>
       <title>The Autobiography of Benjamin Franklin</title>
     </book>
</bookstore>";

Open in new window

0
Comment
Question by:djcheeky
[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
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 22781517
I realize a bit of time has been invested in regex up to this point, but maybe you would be best to use XML-specific code to parse XML.

Consider this code example for comparing XML structures:
http://msdn.microsoft.com/en-us/library/aa302295.aspx

Look for "Click here to download the code sample for this article." link to the code.  Seems to compare to the granularity required for XML.
0
 

Author Comment

by:djcheeky
ID: 22783517
Hi drrudik

I have come to the same conclusion and as a matter of fact started converting the code to use XML Document yesterday. I will have a look at the article as well. Thanks.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22785276
Thanks for the question and the points.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

740 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