Friday, November 21, 2008

Exporting XML Files from Textml

I had a case where documents were being created and stored dynamically in Textml Server by an application, but we wanted the physical files exported. I had a ContentServer class already in place for selecting all documents in a collection and for selecting a document by file name, which would make this easier. This was going nowhere near a production server, so reusing what I had to get this done quickly was my primary concern.

There are some ways to clean this up, but the general approach should be helpful in similar situations.

Here is the method that will return a list of all documents in a collection. This gives me the file name, which I use to get the individual documents.

private List<ListItem> SelectAllTextml()
{
List<ListItem> myList = new List<ListItem>();
string textmlStandardHeader = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><query VERSION=\"3.6\" RESULTSPACE=\"RGuideAdmin\">\n";
string textmlStandardFooter = "</query>";
string textmlCollection = "<property NAME=\"collection\"><elem>" +
this.ContextAdditionalName + "</elem></property>";
string textmlFile = "<property NAME=\"NAME\"><elem><anystr/></elem></property>";
string textmlQuery = textmlStandardHeader + "<andkey>" +
textmlCollection +
textmlFile +
"</andkey>" + textmlStandardFooter;
IxiaClientServices IxiaCS = new IxiaClientServices();
IxiaServerServices IxiaSS = IxiaCS.ConnectServer(this.ContextServer);
IxiaDocBaseServices IxiaDS = IxiaSS.ConnectDocBase(this.ContextContainer);
IxiaSearchServices IxiaSearchS = IxiaDS.SearchServices;
IxiaResultSpace textmlResultSpace = IxiaSearchS.SearchDocuments(textmlQuery);
if (textmlResultSpace.Count > 0)
{
for (int i = 0; i < textmlResultSpace.Count; i++)
{
ListItem documentItem = new ListItem();
IxiaDocument document;
document = textmlResultSpace.Item(i);

MemoryStream xmlStream = new MemoryStream();
document.Content.SaveTo(xmlStream);
xmlStream.Position = 0;
XPathDocument textmlXmlDocument = new XPathDocument(xmlStream);
XPathNavigator textmlXmlNav = textmlXmlDocument.CreateNavigator();
documentItem.Text =
textmlXmlNav.SelectSingleNode("descendant::title[1]").ToString() +
" (" + document.Collection + ")";
documentItem.Value =
textmlXmlNav.SelectSingleNode("descendant::guide[1]").
GetAttribute("id", "");

myList.Add(documentItem);
}
}
return myList;
}


Here is the method that will return a document (or documents) by file name, limited to a collection.

private List<XmlDocument> SelectTextml(string fileName)
{
List<XmlDocument> myList = new List<XmlDocument>();
string textmlStandardHeader = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><query VERSION=\"3.6\" RESULTSPACE=\"RGuideAdmin\">\n";
string textmlStandardFooter = "</query>";
string textmlCollection = "<property NAME=\"collection\"><elem>" +
this.ContextAdditionalName + "</elem></property>";
string textmlFile = "<property NAME=\"NAME\"><elem>" + fileName + "<anystr/></elem></property>";
string textmlQuery = textmlStandardHeader + "<andkey>" +
textmlCollection +
textmlFile +
"</andkey>" + textmlStandardFooter;
IxiaClientServices IxiaCS = new IxiaClientServices();
IxiaServerServices IxiaSS = IxiaCS.ConnectServer(this.ContextServer);
IxiaDocBaseServices IxiaDS = IxiaSS.ConnectDocBase(this.ContextContainer);
IxiaSearchServices IxiaSearchS = IxiaDS.SearchServices;
IxiaResultSpace textmlResultSpace = IxiaSearchS.SearchDocuments(textmlQuery);
if (textmlResultSpace.Count > 0)
{
for (int i = 0; i < textmlResultSpace.Count; i++)
{
IxiaDocument document = textmlResultSpace.Item(i);
MemoryStream xmlStream = new MemoryStream();
document.Content.SaveTo(xmlStream);
xmlStream.Position = 0;
XmlDocument textmlXmlDocument = new XmlDocument();
textmlXmlDocument.Load(xmlStream);
myList.Add(textmlXmlDocument);
}
}

return myList;
}


Here's the method I used to go through each document returned in the collection list and save each to an XML file.

myContentServer = new ContentServer(Server.MapPath("~/App_Data/" + 
ddlProduct.SelectedValue + ".xml"));
List<ListItem> myGuides = myContentServer.SelectAll();
if (myGuides.Count > 0)
{
if (Directory.Exists(Server.MapPath(exportDirectory + "/" +
ddlProduct.SelectedValue)))
{
// Delete the directory and anything existing in it.
Directory.Delete(Server.MapPath(exportDirectory + "/" +
ddlProduct.SelectedValue), true);
}
Directory.CreateDirectory(Server.MapPath(exportDirectory + "/" +
ddlProduct.SelectedValue));
foreach (ListItem guide in myGuides)
{
List<XmlDocument> myGuide = myContentServer.Select(guide.Value);
XmlDocument document = myGuide[0];
// Save the file to the export directory.
document.Save(Server.MapPath(exportDirectory + "/" +
ddlProduct.SelectedValue + "/" + guide.Value + ".xml"));
divExportList.InnerHtml += "<br/>" + exportDirectory + "/" +
ddlProduct.SelectedValue + "/" + guide.Value + ".xml";
}
}

No comments: