Solved

How can I fill in missing XML elements with default values?

Posted on 2004-08-17
3
244 Views
Last Modified: 2012-05-05
I'm developing web applications for in-house use and designed to interact with Microsoft's Customer Relationship Management system. The way the CRM API works with respect to retrieving data is that you call the Retrieve method on a proxy object for the type of object you are trying to retrieve, and specify the guid of the actual instance of that object you are interested in.

For example (pseudocode) :

strOutput = oInvoice.Retrieve( user_authorization, invoiceGuid, elements_you_would_like_returned)

would return an XML string describing the specific invoice who's guid is 'invoiceGuid'. If you leave 'elements_you_would_like_returned', it returns the entire invoice.

HOWEVER (this is the root of my problem), if fields in the invoice object are blank (quite a common occurance), the XML element containing that column's information is missing.

An example:
Typically once the XML string is returned, it is loaded into an XmlDocument. If I wish to display the 'Bill To' address, for instance--

xmlDoc.LoadXML(strOutput) 'output from above
textbox1.Text = xmlDoc("invoice").Item("billto_city").InnerText

-- but if the billto_city field was left blank, this throws an 'Object reference not set to an instance of object' exception.

It is not really feasable to put this inside a Try-Catch block, as I would need to do this for every column in the Invoice object.

I DO have an xsd which describes the CRM Invoice, however, having done some research, I found at http://www.w3.org/TR/xmlschema-0/ that:
 "Default attribute values apply when attributes are missing, and default element values apply when elements are empty." That seems disheartening.

So my question is this: Is there any way to put missing XML elements into the returned string so the application wouldn't throw that exception when I try and print out all the values?

Thanks in advance for any help, and for reading that long and convoluted post.
-adlibdev
0
Comment
Question by:adlibdev
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
rdcpro earned 125 total points
ID: 11823282
One should *always* test for the presence of a node before attempting to access it.  For example, any method that returns a node should be tested to see if it returned null, before trying to look at the InnerText property.  Here's a javascript example (.NET isn't much different, though):

var oNode = xmlDoc.selectSingleNode("foo/bar/snafu");
sMyText = (oNode != null) ? oNode.text : "";

In this case, sMyText equals the value of the snafu element, if snafu exists, and is an empty string if it doesn't.

If you try to combine the two together, like:

sMyText = xmlDoc.selectSingleNode("foo/bar/snafu").text;

you're asking for trouble!  ;^)

But don't try to use try-catch blocks for this, because the overhead is high.

Regards,
Mike Sharp
0
 

Author Comment

by:adlibdev
ID: 11824551
That's not a bad way of doing it. Still a bit tedious, but I guess you can't have everything. Thanks for your help.
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 11827032
Well, that's one of the reasons I like XSLT...

Regards,
Mike Sharp
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP Curl Multi-exec 13 60
xml read and write 3 58
How do I bind the results to a grid 3 23
XML Data Missing in PHP SimpleXML 8 22
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

20 Experts available now in Live!

Get 1:1 Help Now