C# XML Editor - Matching XMLNode

江枫思渺然 提交于 2020-05-16 04:02:08

问题


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

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

I could parse the current XML tag at the cursor/caret position, but I then wouldn't be able to find the parent...

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


回答1:


You can figure it out:

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

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.

Different idea:

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.



来源:https://stackoverflow.com/questions/9777421/c-sharp-xml-editor-matching-xmlnode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!