Monday, August 18, 2008

Atari 400 and My First Dose of FAIL

I'm not sure what made me think about this now, but I was just daydreaming and thought about my very first programming experience.

I had gotten an Atari 400 (may have been XL?) as a kid and it came with a BASIC cartridge and this binder full of programs you could write. I remember looking at them and thinking it was cool that if I type these odd looking lines that didn't make sense to me, the machine would do something. I was fixated with one that I think was shorter, a program that would display an American flag on the TV screen.

One night my father and I typed out the program. If I remember correctly, I typed it out the first time and it didn't run. FAIL. Nice first experience. My father tried it. FAIL. The keyboard was this awful touch-button setup -- not actual keys but areas you would press. Brutal typing. I think this is the model.

I tried again. And again. And again. FAIL. FAIL. FAIL.

I remember ... not being irritated or mad, but disappointed. I wanted it to run and it wouldn't and no one could tell me why. I remember sitting with my face right next to the TV screen going character by character, line by line and checking my work. Never did get it to run.

Hmm. Wonder if I can find that machine somewhere in my parents' basement?

Thursday, July 3, 2008

Read in a String of XML to an XPathDocument in ASP.NET

Had a string representation of an XML file today that needed to be read into an XPathDocument object.

// results was the string XML file representation
StringReader sReader = new StringReader(results);
XmlReader xReader = new XmlTextReader(sReader);
XPathDocument myXpathDocument = new XPathDocument(xReader);

Tuesday, June 24, 2008

Saving an XPathDocument or XmlDocument to a File in ASP.NET

Today I was working on a class that was returning an XPathDocument representation of an XML document and I needed to save it to a file. I switched the class to return an XmlDocument ... and the reason for that should be obvious from the two code samples below.

Here's how I saved an XPathDocument to an XML file:

// myItems[0] is from a generic List<XPathDocument> list
XPathDocument document = myItems[0];
// Feels like there should be an easier way to do this.
XPathNavigator documentNav = document.CreateNavigator();
XmlTextWriter writer = new XmlTextWriter(
Server.MapPath(temporaryFiles + "/" +
fileId + ".xml"), System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 3;
writer.WriteStartDocument();
writer.WriteNode(documentNav, true);
writer.WriteEndDocument();
writer.Close();

Now here's how I saved an XmlDocument to an XML file:

// myItems[0] is from a generic List<XmlDocument> list
XmlDocument document = myItems[0];
document.Save(
Server.MapPath(temporaryFiles + "/" + fileId + ".xml"));

Monday, June 23, 2008

Load an XML File from the File System into Textml with ASP.NET

I'm building an internal application that needs to load an XML file from the file system into a specific repository path in Textml Server. Here's the method I used within a ContentServer class I created.

The references to various this properties are set in the same class where I get the values from a configuration file for the staging and production servers.
public bool Publish(string fileUri)
{
try
{
ArrayList documents = new ArrayList(1);
IxiaClientServices IxiaCS = new IxiaClientServices();
IxiaServerServices IxiaSS = IxiaCS.ConnectServer(this.ContextServer);
IxiaDocBaseServices IxiaDS = IxiaSS.ConnectDocBase(this.ContextContainer);
IxiaDocumentServices ds = IxiaDS.DocumentServices;
IxiaDocument document = IxiaDocument.getInstance();
FileInfo file = new FileInfo(fileUri);

document.Name = file.Name;
document.MimeType = "text/xml";
document.Collection = this.ContextAdditionalName;
document.Content = IxiaDocument.MakeBinaryContent(file.FullName);

documents.Add(document);
IxiaTextmlServerError [] err = ds.SetDocuments(documents,
(int)IxiaDocumentServices.TextmlSetDocuments.TextmlAddDocument |
(int)IxiaDocumentServices.TextmlSetDocuments.TextmlReplaceDocument |
(int)IxiaDocumentServices.TextmlSetDocuments.TextmlIndexDocument,
TextmlDocumentType.TextmlDocument);

// If there is more than one item, and that first item is
// not null or empty, return false.
if (err.Length > 1 && !String.IsNullOrEmpty(err[0].ToString()))
{
// TODO: Log each in the EventViewer
return false;
}
else return true;
}
catch (Exception ex)
{
// TODO: Log in the EventViewer
return false;
}
}

I need to do the same into MarkLogic so I'll post that snippet here as soon as it's done.

Tuesday, June 17, 2008

Using the ASP.NET AdRotator Control for Text-Only Ads

Here's a pathetic little hack to leverage the ASP.NET AdRotator control to generate text-only ads.

In your ASPX page:

<asp:AdRotator
ID="AdRotator1"
AdvertisementFile="~/App_Data/DidYouKnow.config"
runat="server"
OnAdCreated="AdRotator1_CustomAdCreated"/>

<asp:HyperLink
ID="HyperLink1"
runat="server"
NavigateUrl="#"></asp:HyperLink>

In your code-behind page:

public void AdRotator1_CustomAdCreated(object sender, AdCreatedEventArgs e)
{
AdRotator1.Visible = false;
HyperLink1.Text = e.AlternateText;
HyperLink1.NavigateUrl = e.NavigateUrl;
}

In your XML file:

<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<NavigateUrl>target.aspx</NavigateUrl>
<AlternateText>Did You Know Item 1</AlternateText>
</Ad>
<Ad>
<NavigateUrl>target.aspx</NavigateUrl>
<AlternateText>Did You Know Item 2</AlternateText>
</Ad>
<Ad>
<NavigateUrl>target.aspx</NavigateUrl>
<AlternateText>Did You Know Item 3</AlternateText>
</Ad>
</Advertisements>