Is there an easy way to load xml fragments from a string? I can’t seem to find anything on the topic. I really don’t want to have to loop through the string at the first hit of a “<”, memorize the position of that and find the end of the tag.
So an example would be
“this is a test 3 for embedded xml load. Tada!”
So kinda like html will detect
in the text for a tag.
Apparently there aren’t an xml methods that will just parse for a fragment in a string like the above. At least, not that I’ve found.
It’s not going to work in strings like that though. Not really sure what you want from that, if it’s just some tokens to trigger script commands from a string then XML is totally overkill IMO, just use some custom escaped tokens, should take all of 10 minutes to write.
I’m just looking for a solution to easily either strip away the excess outside the tags, or (preferably) just load the XML while ignoring the other text.
No, they’re nothing but strings until you do something with them, and what do you actually mean by “load the XML”? Like, load it into what?
Given your example you almost seem to just want to magically parse and execute c# code from XML strings, that’s not a trivial thing and I really don’t see any valid use case for ever wanting to do that. What’s the end-goal you’re trying to accomplish with this idea, perhaps there’s a better/simpler way to go about it?
Yes, they are in fact tokens in xml. I already have the first layer of the xml reading it in using XmlDocument. So doc.Load(). Take this example xml:
<Dialog>
<Text>this is a test <pause>3</pause> for embedded xml <sendMessage>load</sendMessage>. Tada!</Text>
</Dialog>
And then grab that node’s inner text:
“this is a test 3 for embedded xml load. Tada!”
Now I could go the tedious route of writing my own parser for pulling out tags separately, but I wanted to confirm that there already wasn’t an operation to do that. I don’t see any magic there ;).
Think dialog, and embedded in text are xml tags that can be loaded while the text is appearing in the box. I’ve got everything except the automagically part of being able to grab the xml fragment. I can handle parsing through attributes and values on my own.
Its very simple. Lets just leave out the “why” as to avoid unnecessary background.
/// BEGIN Irrelevant explanation to my previous post
<Dialog>
<Text>this is a test <pause>3</pause> for embedded xml <sendMessage>load</sendMessage>. Tada!</Text>
</Dialog>
So inside the “Text” node is text that has escaped characters. After loading in the above xml into XmlDocument, and then selecting the text node - you can access the inner text. This will un escape the characters and produce the output :
/// END Irrelevant explanation to my previous post
this is a test <pause>3</pause> for embedded xml <sendMessage>load</sendMessage>. Tada
I am simply asking if there is a function or simple way to effectively XmlDocument.Load(“3”) and strip out unimportant text. Possibley first instance of a opening/closing tag or something to that effect.
I only ask this to avoid doing a good bit of string operations.
Regex will be your friend in this endeavor, as I am assuming then that you want to load the value into a string, and then split the string according to whether or not the substring is an XML element. I don’t have the regex handy, but the c# function you’ll be looking to use is documented here.
I linked you a parser that would get you 99% of the way there and would be 10 times faster than using regex. I still don’t get why you need XML for this at all though, because you’re not really using it like XML - which has a robust syntax and grammar. Anyone else using that system might assume it supports all of the syntax of XML (such as multiple nexted elements, attributes etc) but it doesn’t. Seriously the “why” of what you’re trying to accomplish (not the how) is important, I wouldn’t have bother asking otherwise. Seems to me all you need is a custom tokenizer for start/end sets like {replace this text} or [sendMessageHandler] which would only take like hald an hour to code and I would have typed out an example if I was certain that’s all you needed but you seemed determined to dodge that question.
You could have saved days by opening your post by explaining what you actually wanted to do, not what you thought you needed to do.
Unless you really do need the full syntax and grammar of XML, in which case regex is not to good way to go about it and you should just hack into my lightweight XML parser I linked to earlier.
You linked me to an xml parser even though I did not ask for one. Completely irrelevant to my post. I was already using XmlDocument. The xml is already handled, thus the reason I did not ask for help with normal Xml. I don’t really see a problem with an extra mb in a multi gb project, nor would I want to risk running into possible bugs for simple XML operations that are more likely than using what is already included in C#.
And it is not a {replace text here}. I already gave you an example = in game dialog. My post explained exactly what I wanted to do. The post was answered very simply enough without background context. It is very possible that some posters may need a better approach to a problem, but I did not. If a question is asked, and the poster is adamant about using his proposed method - why not just help him to do it? Let him figure out whether its actually wrong or not.
Furthermore, if you stop to think about the power of load xml fragments during a dialog system - you will find that it will allow for a great deal of expanse. Unless you aren’t trying to solve the problem that I am. The post has been answered, there is no reason to progress further off topic. I would just recommend reading the initial post more thoroughly and providing more relevant information.
EDIT:
I guess you are missing the point that I don’t need help actually loading the xml to parse it, but rather I was looking for a way to isolate the xml in context. So the return string will be 3 with any number of attributes for the code I posted. The post I linked to was attempting to returned the inner text, so I included that as well.
I do want the full syntax and grammer of xml
[quote=“SirGive, post:7, topic: 509617, username:SirGive”]
I only ask this to avoid doing a good bit of string operations.
[/quote]
I don’t want to have to do the string parsing
Did you ever stop to think that some of the folks here have a fair amount of experience and maybe they’ve solved a similar problem in the past? Or maybe they did exactly what you did and realized it wasn’t the correct solution and would like to help you by saving you the time and hassle? That kind of stuff happens on this forum all the time.
I would disagree and say that it’s awkward, unwieldy, and error prone actually. It’s also not a valid XML document at that point so all your talk about wanting the syntax and grammar of XML goes out the window.
I’ve got the solution I wanted and its working for me. Plain, simple, and implementation is straight forward for the feature being developed. Some of the folks posting questions aren’t entirely devoid of experience, either. Simple question, and a simple answer to what was asked was all I expected. Sure, throw in advice - but at least read the question.