Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Reply: Converting MSXML2 DOM example over to C#

Date Posted:8/1/2005 11:58 AM
Posted by:
Marilyn Beaudreau

Hi, I have an excel application that from VBA creates an MSXML2 DOM document and posts it to an ASP page which then parses the data.  I need to replace the existing ASP source with a c# .net procedure.  The VB ASP source to create the object follows. How do I do the following in c# .net? I've tried a few things but am having issues with the docReceived object. Can someone get me started in the right direction?


'Create XML DOMDocument object, and load XML data from client


Set docReceived = CreateObject("MSXML2.DOMDocument")


docReceived.async = False


docReceived.load Request


 Set rootNode = docReceived.documentElement


'Get customer and quoteid from XML data packet


customer = rootNode.selectNodes("//customer").item(0).Text


Date Posted:8/1/2005 3:11 PM
Posted by:
David Silverlight

Hello Marylin,

     Let me reply with a code snippet later today.  In the meanwhile, why don't you download my .NET Jeopardy example.  It is full of samples of accessing XML Documents. You can find  it here:


http://www.xmlpitstop.com/ArticleManagement/DisplayArticle.aspx?ResourceID=75


     Kindest regards,
          David Silverlight
          HeadGeek@XMLPitstop.com


Date Posted:9/9/2005 8:53 AM
Posted by:
Marilyn Beaudreau
 I have an excel VBA application that creates and sends an XML DOMDocument to a URL. The URL points to an aspx page written in C#. This page parses the incoming XML as well as send an XML Response back to the VBA excel application. For some reason the response method coming from the server is empty.  I'm not sure how to go about debugging this since I can't even prove that the xml is getting to the aspx page.  Do you see what I'm doing wrong?  Any help is greatly appreciated.


thx,
-Marilyn


Following is the VBA source:

 

 ' build XML string and initialize document
        strXML = "<?xml version='1.0'?><quote></quote>"
       
        'Setup XML
        Set docSubmit = CreateObject("MSXML2.DOMDocument")
        docSubmit.async = False
        docSubmit.loadXML strXML
       
        ' build Customer node
        Set customerText = docSubmit.createTextNode(Org_Name)
        Set customerElem = docSubmit.createElement("customer")
        customerElem.appendChild customerText
       
        ' build Quote ID node
        Set quoteIdText = docSubmit.createTextNode(QuoteNumber)
        Set quoteIdElem = docSubmit.createElement("quote_id")
        quoteIdElem.appendChild quoteIdText
       
        ' build Quote Date node
        Set quoteDateText = docSubmit.createTextNode(QuoteDate)
        Set quoteDateElem = docSubmit.createElement("quote_date")
        quoteDateElem.appendChild quoteDateText

 

     Set myNode = docSubmit.selectSingleNode("//quote")
        myNode.appendChild customerElem
        myNode.appendChild quoteIdElem
        myNode.appendChild quoteDateElem

 


        'Create XML poster object and post XML data to server
        Set poster = CreateObject("MSXML2.XMLHTTP")
        poster.Open "POST", "
http://localhost/ExcelReader/QuoteLoad.aspx", False
        poster.send docSubmit
       
        'Display response from server
        Set docResponse = CreateObject("MSXML2.DOMDocument")
        Set docResponse = poster.responseXML
                      
              MsgBox "Info coming back: " & docResponse  '*This is always coming back empty!?

 


--------------------------------------------------------------------------------------------------
following is the c# source from QuoteLoad.aspx

 

  private void Page_Load(object sender, System.EventArgs e)
  {
  XmlDocument xDoc = null; 
   xDoc = new XmlDocument();

 

   try
   {
    xDoc.Load(Request.InputStream); // Load the xml file from the excel app.

    // XPath statement for selecting nodes 
    string xpath = "//quote/*";

  // Select the nodes with the XPath query
    XmlNodeList nodes = xDoc.SelectNodes(xpath);
    XmlElement rootNode = xDoc.DocumentElement;

   //'Get customer and quoteid from XML data packet
    string customer = rootNode.SelectNodes("//customer").Item(0).Value;
    string quote_id = rootNode.SelectNodes("//quote_id").Item(0).Value;
    string quote_date = rootNode.SelectNodes("//quote_date").Item(0).Value;

 

    //Call method that sends XML Response back to caller

 

    XMLReply("Success");

 

//     Response.Write(xDoc.OuterXml);
//     Response.Write(Request.InputStream.ToString());
     }
   catch(Exception ex)
   {
    Response.Write("Error during XML read" + ex.Message);
   }


  }

 

 public void XMLReply(string msg)
  { //send XML Response back to caller

 

  //Build XML string and initialize document
  string strXML = "<?xml version='1.0'?><Status></Status>";
      
  MSXML2.DOMDocument docSubmit = new MSXML2.DOMDocument();
  docSubmit.async = false;
  docSubmit.loadXML(strXML);
 

 

  //Build Error Msg Node
  XmlText  errorText = (XmlText)docSubmit.createTextNode(msg);
  XmlElement errorElem = (XmlElement)docSubmit.createElement("error");
  errorElem.AppendChild(errorText);
//
//  //Build URL Node
//  XmlText  urlText = (XmlText)docSubmit.createTextNode(q2cUrl);
//  XmlElement urlElem = (XmlElement)docSubmit.createElement("url");
//  urlElem.AppendChild(urlText);
//
//  //Append Nodes to Document
  XmlNode xmlNode = (XmlNode)docSubmit.selectSingleNode("//Status");
  xmlNode.AppendChild(errorElem);
//  xmlNode.AppendChild(urlElem);

  //Send it
  Response.Write(docSubmit.xml);
  Response.End();
   }

Date Posted:9/9/2005 8:54 AM
Posted by:
Marilyn Beaudreau
 I have an excel VBA application that creates and sends an XML DOMDocument to a URL. The URL points to an aspx page written in C#. This page parses the incoming XML as well as send an XML Response back to the VBA excel application. For some reason the response method coming from the server is empty.  I'm not sure how to go about debugging this since I can't even prove that the xml is getting to the aspx page.  Do you see what I'm doing wrong?  Any help is greatly appreciated.


thx,
-Marilyn


Following is the VBA source:

 

 ' build XML string and initialize document
        strXML = "<?xml version='1.0'?><quote></quote>"
       
        'Setup XML
        Set docSubmit = CreateObject("MSXML2.DOMDocument")
        docSubmit.async = False
        docSubmit.loadXML strXML
       
        ' build Customer node
        Set customerText = docSubmit.createTextNode(Org_Name)
        Set customerElem = docSubmit.createElement("customer")
        customerElem.appendChild customerText
       
        ' build Quote ID node
        Set quoteIdText = docSubmit.createTextNode(QuoteNumber)
        Set quoteIdElem = docSubmit.createElement("quote_id")
        quoteIdElem.appendChild quoteIdText
       
        ' build Quote Date node
        Set quoteDateText = docSubmit.createTextNode(QuoteDate)
        Set quoteDateElem = docSubmit.createElement("quote_date")
        quoteDateElem.appendChild quoteDateText

 

     Set myNode = docSubmit.selectSingleNode("//quote")
        myNode.appendChild customerElem
        myNode.appendChild quoteIdElem
        myNode.appendChild quoteDateElem

 


        'Create XML poster object and post XML data to server
        Set poster = CreateObject("MSXML2.XMLHTTP")
        poster.Open "POST", "
http://localhost/ExcelReader/QuoteLoad.aspx", False
        poster.send docSubmit
       
        'Display response from server
        Set docResponse = CreateObject("MSXML2.DOMDocument")
        Set docResponse = poster.responseXML
                      
              MsgBox "Info coming back: " & docResponse  '*This is always coming back empty!?

 


--------------------------------------------------------------------------------------------------
following is the c# source from QuoteLoad.aspx

 

  private void Page_Load(object sender, System.EventArgs e)
  {
  XmlDocument xDoc = null; 
   xDoc = new XmlDocument();

 

   try
   {
    xDoc.Load(Request.InputStream); // Load the xml file from the excel app.

    // XPath statement for selecting nodes 
    string xpath = "//quote/*";

  // Select the nodes with the XPath query
    XmlNodeList nodes = xDoc.SelectNodes(xpath);
    XmlElement rootNode = xDoc.DocumentElement;

   //'Get customer and quoteid from XML data packet
    string customer = rootNode.SelectNodes("//customer").Item(0).Value;
    string quote_id = rootNode.SelectNodes("//quote_id").Item(0).Value;
    string quote_date = rootNode.SelectNodes("//quote_date").Item(0).Value;

 

    //Call method that sends XML Response back to caller

 

    XMLReply("Success");

 

//     Response.Write(xDoc.OuterXml);
//     Response.Write(Request.InputStream.ToString());
     }
   catch(Exception ex)
   {
    Response.Write("Error during XML read" + ex.Message);
   }


  }

 

 public void XMLReply(string msg)
  { //send XML Response back to caller

 

  //Build XML string and initialize document
  string strXML = "<?xml version='1.0'?><Status></Status>";
      
  MSXML2.DOMDocument docSubmit = new MSXML2.DOMDocument();
  docSubmit.async = false;
  docSubmit.loadXML(strXML);
 

 

  //Build Error Msg Node
  XmlText  errorText = (XmlText)docSubmit.createTextNode(msg);
  XmlElement errorElem = (XmlElement)docSubmit.createElement("error");
  errorElem.AppendChild(errorText);
//
//  //Build URL Node
//  XmlText  urlText = (XmlText)docSubmit.createTextNode(q2cUrl);
//  XmlElement urlElem = (XmlElement)docSubmit.createElement("url");
//  urlElem.AppendChild(urlText);
//
//  //Append Nodes to Document
  XmlNode xmlNode = (XmlNode)docSubmit.selectSingleNode("//Status");
  xmlNode.AppendChild(errorElem);
//  xmlNode.AppendChild(urlElem);

  //Send it
  Response.Write(docSubmit.xml);
  Response.End();
   }

Date Posted:9/10/2005 11:28 AM
Posted by:
David Silverlight
Hello Marylin,

     Well, here are a few thoughts.

a) Try hardcoding an xml string into your aspx page that matches the xml that you would be passing into it and call the page by itself to see what it returns.  This may prove helpful in debugging.  If it returns what you are expecting, then call it from the VBA script and see if it works.
b) This sounds really ideal to be implemented as a web service.  You can call a web service using a POST and it will work just the same.  It will also give you a better interface for testing the web service.  Have you considered implementing QuoteLoad as a web service?

     Kindest regards,
          David

Date Posted:9/12/2005 8:43 AM
Posted by:
Marilyn Beaudreau
thanks Dave for the good suggestions. I will try hardcoding an XML value and see what result I get. Setting this up as a web service is also a really good idea. I can see in the future where that would really be helpful.  Thanks for helping me look forward a little! ;-)
I'll keep you posted...

- marilyn

Date Posted:9/12/2005 5:07 PM
Posted by:
Marilyn Beaudreau
Hi Dave,

Well I made some progress in that if I create the xml as a string within the .NET page I can parse it without any problem. BUT when I have the excel app in place and use the xmlDoc.Load(Request.InputStream)  statement things fall apart! Have you ever read xml from a posted stream? I've tried so many things today!  I've looked for an example on-line that does this without much luck. Any info you can provide is appreciated.

thx,
- marilyn

Date Posted:9/13/2005 2:57 AM
Posted by:
David Silverlight
Hello Marilyn,

   Well, it is good to hear that it worked for the hardcoded XML.  It seems like we really need to just focus on loading the xml object using the request.InputStream.  Try the code below to load up the xml object.


XmlReader reqStreamReader = new XmlTextReader(Request.InputStream);


XmlDocument ExcelDoc = new XmlDocument();


ExcelDoc.load(reqStreamReader);

From what I can see, you have everything setup correctly and since it seems to work well, if the XML is hardcoded, then the architecture seems sound, it is just a matter of grabbing the XML that you are sending into the page.  If you are able to set a break point in the code above, you will also be able to see if the xml that you are passing in is what you are expecting.  Let me know if this helps. 

     Kindest regards,
          David Silverlight
          HeadGeek@XMLPitstop.com

Date Posted:5/18/2006 3:48 AM
Posted by:
Carl Larsen
Hello All,

I have an XML example I'm trying to convert to VBA for use in MS Access
I'm totally lost, Don't know where to begin. Here is the sample code provided.
I hope someone can help me out with this.

Sample Data...
When sending the XML request, the Web Tool name must be specified. The Web Tool name is RateV2. The format of the XML transaction is:
http://servername/ShippingAPITest.dll?API=RateV2&XML=<RateV2Request USERID="username" PASSWORD="password">…….</RateV2Request>

'Test Request
<RateV2Request USERID="xxxxxx" PASSWORD="xxxxxxx">
<Package ID="0">
<Service>All</Service>
<ZipOrigination>10022</ZipOrigination>
<ZipDestination>20008</ZipDestination>
<Pounds>10</Pounds>
<Ounces>5</Ounces>
<Size>LARGE</Size>
<Machinable>TRUE</Machinable>
</Package>
</RateV2Request>


'Response to Test Request
<?xml version="1.0" ?>
<RateV2Response>
<Package ID="0">
<ZipOrigination>10022</ZipOrigination>
<ZipDestination>20008</ZipDestination>
<Pounds>10</Pounds>
<Ounces>5</Ounces>
<Size>LARGE</Size>
<Machinable>TRUE</Machinable>
<Zone>3</Zone>
<Postage>
<MailService>Express Mail PO to Addressee</MailService>
<Rate>39.20</Rate>
</Postage>
<Postage>
<MailService>Priority Mail</MailService>
<Rate>11.05</Rate>
</Postage>
<Postage>
<MailService>Parcel Post</MailService>
<Rate>8.61</Rate>
</Postage>
<Postage>
<MailService>Bound Printed Matter</MailService>
<Rate>3.53</Rate>
</Postage>
<Postage>
<MailService>Media Mail</MailService>
<Rate>5.14</Rate>
</Postage>
<Postage>
<MailService>Library Mail</MailService>
<Rate>4.91</Rate>
</Postage>
</Package>
</RateV2Response>

Scripted Test Error Responses
If any values were changed in your request, the following is an example of an error that can occur. The following error shows an example of using the Test with an invalid container value.
<?xml version="1.0" ?>
<Error>
<Number>-2147219488</Number>
<Source>Rate_Respond;SolServerRatesTest.RateV2_Respond
</Source>
<Description>Invalid value for container.</Description>
<HelpFile />
<HelpContext>1000440</HelpContext>
</Error>
Although the input may be valid, the response will still raise this error, because those particular values have not been included in this test server.

MANY THANKS...
Carl
 





Advertise on XMLPitstop


4,707 Total Members
5 members(last 30 days)
1 members(last 7 days)
1 members(today)

2,003 Total Discussions
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

47,487 Total Blog Posts
0 Blogs(last 30 days)
0 Blogs(last 7 days)
0 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

14,424 Total Resources
3 Resources(last 30 days)
0 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139