Solved

XML Analysis using C# and Regex

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to reference objects of the calling class without passing them as parameters 29 176
Question! 4 36
C# bracket error 3 31
Hey!!!! 1 22
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

777 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