SuperDesign/Source/开发辅助工具/Tools/FrmXpath.cs
鑫Intel c7b33920bd ### 2023-01-21更新
------
#### SuperDesign    V2.0.2301.2101
- *.[新增]新增API编辑器,自动保存和快速插入修改参数。
- *.[改进]使用新款高亮编辑器。
2023-01-21 23:28:09 +08:00

113 lines
3.9 KiB
C#

using HtmlAgilityPack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace .Tools
{
public partial class FrmXpath : DockContent
{
public FrmXpath()
{
InitializeComponent();
CbbXMLType.SelectedIndex = 0;
highlightEditor1.ReadOnly = true;
}
private void RySearchXpath_OnSearch(object sender, EventArgs e)
{
string text = "";
var format = "";
try
{
if (CbbXMLType.SelectedIndex == 0)
{
format="xml";
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
xml.LoadXml(TxtXML.Text);
var node = xml.SelectNodes(RySearchXpath.Text);
if (node != null)
{
for (int i = 0; i < node.Count; i++)
{
if (text != "") { text += "\r\n\r\n"; }
text += node[i].OuterXml;
}
}
}
else if (CbbXMLType.SelectedIndex == 1)
{
format = "html";
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(TxtXML.Text);
HtmlNodeCollection anchors = htmlDoc.DocumentNode.SelectNodes(RySearchXpath.Text);
if (anchors != null)
{
for (int i = 0; i < anchors.Count; i++)
{
if (text != "") { text += "\r\n\r\n"; }
text += anchors[i].OuterHtml;
}
}
}
else if (CbbXMLType.SelectedIndex == 2)
{
format = "json";
try
{
JObject jo = (JObject)JsonConvert.DeserializeObject(TxtXML.Text);
var list= jo.SelectTokens(RySearchXpath.Text).ToList();
for (int i = 0; i < list.Count; i++)
{
if (text != "") { text += "\r\n\r\n"; }
text += list[i].ToString();
}
}
catch (Exception ex)
{
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
highlightEditor1.SetHightlightText(text,format);
}
private void FrmXpath_Load(object sender, EventArgs e)
{
highlightEditor1.SetFocus();
}
private void TxtXML_DoubleClick(object sender, EventArgs e)
{
ryControls.Controls.RichTextBox2 txt = (ryControls.Controls.RichTextBox2)sender;
.Controls.FrmText frm = new Controls.FrmText
{
Icon = Icon
};
frm.richTextBox1.Text = txt.Text;
if (frm.ShowDialog() == DialogResult.OK)
{
txt.Text = frm.richTextBox1.Text;
}
}
private void CbbXMLType_SelectedIndexChanged(object sender, EventArgs e)
{
if (CbbXMLType.SelectedIndex == 2) { label1.Text = "JPath表达式"; }
else { label1.Text = "XPath表达式"; }
}
}
}