Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
What to do About Bad KB Code?

I'd rather have bad KB code than none at all, but the following article's code is terrible!  Stuff is commented out, like someone's in mid thought.  There's stale comments that don't have anything to do with the code that's obviously from some other sample code.  It's driving me crazy! 

http://support.microsoft.com/default.aspx?scid=kb;en-us;316337 

This artilcle is a how to on converting a dataset to an ADO Recordset XML and is in VB.NET so I'm porting it to C#. I'm going to post the ported code when I'm done. 


Posted 03-22-2004 12:26 PM by Brendan Tompkins

[Advertisement]

Comments

David Cumps wrote re: What to do About Bad KB Code?
on 03-22-2004 8:16 AM
' Method Name : ConvertToRs
Private Sub HackADOXML

Yes, very great :p It really makes you read through every line of code ;)

How does the code get checked right now before it gets entered? Maybe it needs a bit more checking, it should be very nice and clean code, coming from MS, high-standards and stuff
TJ wrote re: What to do About Bad KB Code?
on 03-22-2004 8:17 AM
Whoever did that should be fired.

I don tthink you should ever see the word "hack" in any KB document. Thats just stupid
Brendan Tompkins wrote re: What to do About Bad KB Code?
on 03-22-2004 8:41 AM
Ha ha ha... Did anyone notice that the method name in the comments is ConvertToRs, but in code it's HackADOXML ??

'********************
' Method Name : ConvertToRs
' Description : The XSLT does not tranform with fullendelements. For example,
' <root attr=""/> intead of <root attr=""><root/>. ADO Recordset
' cannot read this. This method is used to convert the
' elements to have fullendelements.
'********************
Private Sub HackADOXML(ByRef wrt As XmlTextWriter, ByVal ADOXmlStream As System.IO.MemoryStream)
Raymond Chen wrote re: What to do About Bad KB Code?
on 03-22-2004 9:22 AM
"I'd rather have bad KB code than none at all".

What if the "bad"ness creates a security vulnerability?
Brendan Tompkins wrote re: What to do About Bad KB Code?
on 03-22-2004 12:13 PM
Yeah. I guess you're right. Ususally I have to re-write sample code anyhow, so level of debugged isn't as much of a concern. I was trying to be nice to them, I want to get a MVP someday... ;)
Sean M. wrote re: What to do About Bad KB Code?
on 01-17-2005 9:50 AM
I am looking for the ported code. Do you have it posted yet?

Thanks.
Brian Trexler wrote re: What to do About Bad KB Code?
on 12-16-2005 9:32 PM
<soapbox>
Sorry to all those VB guys out there but anyone providing only VB in the .NET world is lame. It's not surprising that many of us are looking for a C# port of this code. After all, most of the world is finally onboard with the C++ style syntax... C/C++, Java, JavaScript, C#, etc. Please lose the VB and give us more C#, Microsoft! Real developers don't need wordy languages!
</soapbox>

I'm porting this code as well. I'll try to post it as soon as I'm done. As I am in the process of porting it, I shake my head at the stupid coding techniques practices. Just in the first method and I'm seeing return codes for successful completion. Did VB just wake up to exception handling?

Can ya tell I detest VB? I'll post the C# when I'm done.

Brian
Brian Trexler wrote re: What to do About Bad KB Code?
on 12-16-2005 10:09 PM
Ok, completely untested but it compiles!!

Hey, the least I could do is post the raw port of the code to save everyone some time. Please test this and correct any issues because I haven't yet (and its late and I'm tired). Good luck!

Brian



using System;
using System.Data;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;

namespace Merrill.Cms.Data
{
public class AdoTransformer
{
public void GetAdoRecordset(DataSet ds, string dbname, string xslfile, string outputfile)
{
XmlTextWriter xwriter = new XmlTextWriter(outputfile, System.Text.Encoding.Default);

this.WriteADONamespaces(xwriter);
this.WriteSchemaElement(ds, dbname, xwriter);

MemoryStream TransformedDatastrm = new MemoryStream();

TransformedDatastrm = TransformData(ds, xslfile);

this.HackADOXML(xwriter, TransformedDatastrm);

xwriter.Flush();
xwriter.Close();
}

private void WriteADONamespaces(XmlTextWriter writer)
{
writer.WriteStartElement("", "xml", "");
writer.WriteAttributeString("xmlns", "s", null, "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
writer.WriteAttributeString("xmlns", "dt", null, "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
writer.WriteAttributeString("xmlns", "rs", null, "urn:schemas-microsoft-com:rowset");
writer.WriteAttributeString("xmlns", "z", null, "#RowsetSchema");
writer.Flush();
}

private void WriteSchemaElement(DataSet ds, string dbname, XmlTextWriter writer)
{
writer.WriteStartElement("s", "Schema", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
writer.WriteAttributeString("id", "RowsetSchema");
writer.WriteStartElement("s", "ElementType", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
writer.WriteAttributeString("name", "", "row");
writer.WriteAttributeString("content", "", "eltOnly");
writer.WriteAttributeString("rs", "updatable", "urn:schemas-microsoft-com:rowset", "true");

this.WriteSchema(ds, dbname, writer);

writer.WriteFullEndElement();
writer.WriteFullEndElement();
writer.Flush();
}

private void WriteSchema(DataSet ds, string dbname, XmlTextWriter writer)
{
int i=1;

foreach(DataColumn dc in ds.Tables[0].Columns)
{
dc.ColumnMapping = MappingType.Attribute;

writer.WriteStartElement("s", "AttributeType", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
writer.WriteAttributeString("name", "", dc.ToString());
writer.WriteAttributeString("rs", "number", "urn:schemas-microsoft-com:rowset", i.ToString());
writer.WriteAttributeString("rs", "baseCatalog", "urn:schemas-microsoft-com:rowset", dbname);
writer.WriteAttributeString("rs", "baseTable", "urn:schemas-microsoft-com:rowset", dc.Table.TableName.ToString());
writer.WriteAttributeString("rs", "keycolumn", "urn:schemas-microsoft-com:rowset", dc.Unique.ToString());
writer.WriteAttributeString("rs", "autoincrement", "urn:schemas-microsoft-com:rowset", dc.AutoIncrement.ToString());
writer.WriteStartElement("s", "datatype", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
writer.WriteAttributeString("dt", "type", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882", GetDataType(dc.DataType.ToString()));
writer.WriteAttributeString("dt", "maxlength", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882", dc.MaxLength.ToString());
writer.WriteAttributeString("rs", "maybenull", "urn:schemas-microsoft-com:rowset", dc.AllowDBNull.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();

i += 1;
}
}

private string GetDataType(string dtype)
{
switch(dtype)
{
case "System.Int32":
return "int";
case "System.DateTime":
return "dateTime";
default:
return "";
}
}

private MemoryStream TransformData(DataSet ds, string xslfile)
{
MemoryStream instream = new MemoryStream();
MemoryStream outstream = new MemoryStream();

ds.WriteXml(instream, XmlWriteMode.IgnoreSchema);
instream.Position = 0;

XslTransform xslt = new XslTransform();
xslt.Load(xslfile);

XmlTextReader xmltr = new XmlTextReader(instream);
XPathDocument xpathdoc = new XPathDocument(xmltr);
XPathNavigator nav = xpathdoc.CreateNavigator();
XsltArgumentList xslArg = new XsltArgumentList();

xslArg.AddParam("tablename", "", ds.Tables[0].TableName);

xslt.Transform(nav, xslArg, outstream);

xslt = null;
instream = null;
xpathdoc = null;
nav = null;

return outstream;
}

private void HackADOXML(XmlTextWriter wrt, System.IO.MemoryStream ADOXmlStream)
{
ADOXmlStream.Position = 0;
XmlTextReader rdr = new XmlTextReader(ADOXmlStream);
MemoryStream outStream = new MemoryStream();

rdr.MoveToContent();

while(rdr.ReadState != ReadState.EndOfFile)
{
if(rdr.Name.CompareTo("s:Schema") == 0)
{
wrt.WriteNode(rdr, false);
wrt.Flush();
}
else if(rdr.Name.CompareTo("z:row") == 0 && rdr.NodeType == XmlNodeType.Element)
{
wrt.WriteStartElement("z", "row", "#RowsetSchema");
rdr.MoveToFirstAttribute();
wrt.WriteAttributes(rdr, false);
wrt.Flush();
}
else if(rdr.Name.CompareTo("z:row") == 0 && rdr.NodeType == XmlNodeType.EndElement)
{
wrt.WriteEndElement();
wrt.Flush();
}
else if(rdr.Name.CompareTo("rs:data") == 0 && rdr.NodeType == XmlNodeType.Element)
{
wrt.WriteStartElement("rs", "data", "urn:schemas-microsoft-com:rowset");
}
else if(rdr.Name.CompareTo("rs:data") == 0 && rdr.NodeType == XmlNodeType.EndElement)
{
wrt.WriteEndElement();
wrt.Flush();
}

rdr.Read();
}

wrt.WriteEndElement();
wrt.Flush();
}
}
}
NoOne wrote re: What to do About Bad KB Code?
on 01-16-2007 11:56 AM

C# is lame, I'm tired of VB Bashing.  Get a life.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?