Avatar of Emergent ESI
Emergent ESI
Flag for United States of America asked on

Creating and loading and Server.CreateObject("Microsoft.XMLDOM") in C#

Hello Everyone,

Can someone please help me figure out what is the equivalent C# code for the following:

set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.load(Request)

I am trying to receive an xsl as dom object and I thought this might work

XslCompiledTransform xslDoc = (XslCompiledTransform)Server.CreateObject("MSXML2.DOMDocument.3.0");

but I am not sure how to load the Request.  I want to use this as part of an ASP.Net page.  

Thanks.

.NET ProgrammingASP.NETC#

Avatar of undefined
Last Comment
Emergent ESI

8/22/2022 - Mon
madhevan_pillai

madhevan_pillai

This may help you. This was from that site


A simpler solution to use the XML DOM object:

From Project/Add Reference/COM add a reference to Microsoft XML v4.0.

SXML2.DOMDocument40 doc = new DOMDocument40Class();
 
When using the doc object you will see listed all its methods:
doc.load, doc.loadXML, doc.transformNode
Carl Tawn

How are you retrieving the transform, is it coming from disk or from a Http request? Either way, since its a .Net app you will want to avoid using COM objects. You can do everything you need with the built-on System.Xml namespace objects.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Emergent ESI

ASKER
carl_tawn:  I am getting the xslt from an http request.  

So I still need to load the Request Object.  Can you guys help me with that part please.

xslDoc.Load(???)

How do I get the Request object?

Sorry for the newbie questions.  Thanks again.
Carl Tawn

Well, as a very simplistic example, you want something along the lines of:
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://some.url.com/test.xsl");
HttpWebResponse res = (HttpWebResponse)req.GetResponse();

// load the response into an XmlReader
XmlTextReader reader = new XmlTextReader(res.GetResponseStream());

// prepare the compiled transform
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(reader);

Open in new window

Emergent ESI

ASKER
carl_tawn:  Thank you so much.  Just one more question.  If a client is sending me the xsl via a DOM document, how would I receive the Dom Object.  From you code, I gather that

the code makes the initial call to http://some.url.com/test.xsl and would accept the response with HttpWebResponse.

I need to wait for a client to connect to my site.  When they connect, they will be sending me a xsl using DOM.  I am trying to figure out how to accept the object.  Thanks and I am sorry for the confusion.

Thanks again.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Carl Tawn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Emergent ESI

ASKER
Thanks carl_tawn.  

I will be testing this and will get back to you.  I have another question.  

If I do the following:

      XmlTextReader reader = new XmlTextReader(Request.InputStream);

Do I still need to create the

   Server.CreateObject("MSXML2.DOMDocument.3.0")

or do I need the following?
     
    SXML2.DOMDocument40 doc = new DOMDocument40Class();

I think the XmlTextReader takes care of this but I wanted to check.

Also,  Do you have any suggesting on how to test this locally?  I will not be able to test this for real with the actual client until Monday.

Thanks again you are a life saver!.

Carl Tawn

No you don't need to. MSXML is the old COM based Xml object model. The .Net framework provides its own set of objects for dealing with Xml (XmlTextReader is one of them). This should be the preferred method of dealing with XML in your .Net apps otherwise you create a dependency on an external library, which you generally want to avoid if you can.
Emergent ESI

ASKER
Carl_tawn:
Does this still apply if the client is not a .Net client?  In other words, can I still just use XmlTextReader and not use Server.CreateObject("Microsoft.XMLDOM") .


Thanks again.

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Carl Tawn

You should be able to. The client app should just be trying to send the raw xml rather than trying to actually send an xmldom object so all you will need to do is read that data.
Emergent ESI

ASKER
Thank you soo much!
It worked.

Thanks.