XML Notepad is good but it just doesn't fit my need...

Updated @ 2008-12-06: XML Notepad 2.5 already supports getting XML data from clipboard, thank Ah Phu.

I wrote a workflow system and it stores the form context and flow context in XML format.  While trouble-shooting, I often need to query context data and analyze the XML string.  When I use SQL Server Management Studio or my customize debugger to fetch the context data, what I get is a XML string.  But XML Notepad only accepts XML files, so I have to press ctrl-C to copy the string, open notepad, paste the XML string, save it as a file and open it  from XMLNotepad, quite a long road. 

I have seen another tool, XMLSpy, it can accept XML string from clipboard and parse it immediately.  Yeah, that's what I need, but XMLSpy is quite expensive.  In the other way, XMLSpy is too rich for me, what I really need is a XML Notepad which can import XML string from clipboard directly.

Thanks God, XML Notepad is open source.  I downloaded the source code from CodePlex and added two menu items, wrote a couple of  code, my dream XML Notepad is coming...

private void toolStripMenuItem13_Click(object sender, EventArgs e)
{
    this.xmlTreeView1.CancelEdit();
    //Save the text form clipboard as file
    string rawXml = Clipboard.GetText();
    //Validate the XML data
    XmlDocument xd = new XmlDocument();
    try
    {
        //Remove <?xml> declaration
        if (rawXml.StartsWith("<?xml version"))
            rawXml =
                System.Text.RegularExpressions.Regex.Replace(
                rawXml, "[<][?]xml.+?[?]>", "");
        if (!rawXml.StartsWith("<"))
            rawXml = rawXml.Substring(rawXml.IndexOf("<"));
        //Use LoadXml to test if the XML is valid
        xd.LoadXml(rawXml);
        //Save it as a temp file
        string fileName = System.IO.Path.GetTempFileName() + ".xml";
        File.WriteAllText(fileName, Clipboard.GetText());
        //Call internal method to open the file
        InternalOpen(fileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Invliad XML document.");
    }
}
 
private void toolStripMenuItem14_Click(object sender, EventArgs e)
{
    //Commit changes
    this.xmlTreeView1.Commit();
    //Prepare the XmlTextWriter
    MemoryStream ms = new MemoryStream();
    XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
    xtw.Formatting = Formatting.Indented;
    //Dump XML to the XmlTextWriter
    this.model.Document.Save(xtw);
    xtw.Flush();
    xtw.Close();
    //Send it to the clipboard
    Clipboard.SetText(Encoding.UTF8.GetString(ms.ToArray()));
}

 


Comments

# by Ian Thomas

Interesting. How far have you progressed? Do you have a Visual Studio project (source) or your modified binaries, that can be downloaded? Would you like some testing? (Sorry, I talk English only)

# by Jeffrey

To Ian, here are the modified files (MainForm.cs & MainForm.res) and the binaries. Check it out! http://blog.darkthread.net/files/folders/3094/download.aspx PS: the modified source code is for XMLNotepad 2.5

# by An Phu

The paste functionality works fine in version 2.5. I copied some xml to my clipboard and hit Ctl+V in XmlNotepad and the xml tree is displayed.

# by Jeffrey

to Ah Phu, you are correct, Xml Notepad can get XML data from clipboard and parse it immediately. The only missing part is copying Xml output to clipboard, and I have updated my content, thanks.

Post a comment