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

 

歡迎推文分享:
Published 24 October 2007 09:01 PM 由 Jeffrey
Filed under: ,
Views: 27,602



意見

# Ian Thomas said on 22 May, 2008 08:02 PM

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)

# Jeffrey said on 23 May, 2008 05:14 AM

To Ian, here are the modified files (MainForm.cs & MainForm.res) and the binaries. Check it out!

blog2.darkthread.net/.../download.aspx

PS: the modified source code is for XMLNotepad 2.5

# An Phu said on 04 December, 2008 06:00 PM

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.

# Jeffrey said on 05 December, 2008 03:52 PM

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.

你的看法呢?

(必要的) 
(必要的) 
(選擇性的)
(必要的) 
(提醒: 因快取機制,您的留言幾分鐘後才會顯示在網站,請耐心稍候)

5 + 3 =

搜尋

Go

<October 2007>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
 
RSS
創用 CC 授權條款
【廣告】
twMVC
最新回應

Tags 分類檢視
關於作者

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

文章典藏
其他功能

這個部落格


Syndication