Wednesday, October 24, 2007 - 文章

XML Notepad Improvement

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()));
}

 

搜尋

Go

<October 2007>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
 
RSS
【工商服務】
最新回應

Tags 分類檢視
關於作者

一個醉心技術又酷愛分享的Coding魔人,十年的IT職場生涯,寫過系統、管過專案, 也帶過團隊,最後還是無怨無悔地選擇了技術鑽研這條路,近年來則以做一個"有為的中年人"自許。

文章典藏
其他功能

這個部落格


BlogLook Score and Rank

Syndication