Solved

XML Analysis using C# and Regex

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…

690 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