Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

XML Analysis using C# and Regex

Posted on 2008-10-22
3
Medium Priority
?
337 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 1500 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
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…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

564 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