问题
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