C#XML编辑器-匹配XMLNode [英] C# XML Editor - Matching XMLNode

查看:71
本文介绍了C#XML编辑器-匹配XMLNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前要从文本位置查找XMLNode.

I'm currently wanting to find out an XMLNode from a text location.

我在文本框中有一个XML文件:

I have a XML file in a textbox:

<Assets id="assets">
   <Asset id="background_asset" url="images/rooms/office/background.jpg" />
</Assets>

并且我想从文本框中的光标/插入位置获取当前节点名称

and I'm wanting to fetch the current node name from my cursor/caret location within the textbox

我可以在光标/插入符号位置解析当前的XML标签,但随后我将无法找到父XML ...

我想知道是否有一种方法可以从XMLDocument获取XMLNode

I'm wondering if there is a way of getting the XMLNode from an XMLDocument

推荐答案

您可以弄清楚:

1)首先从当前光标位置开始,获得光标左侧的第一个< 符号.

1) First from current cursor position, get the first < symbol to the left of the cursor.

2)然后,一个正则表达式从< TagName 中获取标签名称.

2) Then a Regex grabbing the tag name from the <TagName.

然后,您将有足够的导航到该名称的节点.然后,如果该名称有多个节点,则需要比较内容.然后,您可以使用该节点获取其父节点,等等.

You'd then have enough to navigate to a node by that name. You would then want to compare contents if there are more than one node by that name. Then you can use that node to get its parent, etc.

不同的想法:

获取xml文档的每个节点,剥离其子元素,通过 .ToString()输出并获取其长度,然后根据文本长度设置节点索引.然后从光标位置开始,以字符为单位从文档的开始位置确定位置,并从索引中确定哪个节点.

Get each node of your xml document, strip it of child elements, output it via .ToString() and get its length, then set up an index of nodes by text length. Then from the cursor position get its position from the start of the document in characters and determine which node from the index.

这篇关于C#XML编辑器-匹配XMLNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆