Solved

XML Analysis using C# and Regex

Posted on 2008-10-22
3
323 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now