Interesting problem with XML nodes
-
Hi, I have a little issue with some code I'm doing. Basically I have a source xml document that looks like this: This is a test line with these:Keyword alphaKeyword Bravo and the problem is this, if I wanted my code to tag a specific word for example replace the word alpha with alpha to create this new node: This is a test line with these:Keyword alphaKeyword Bravo is there a way of identifying a specific point in the overall line to correctly replace it, given that the word alpha could conceivably appear before or after the instance to be tagged? At the moment I am attempting to use a function that pulls the Paragraph Node to text (innertext) and clears out everything after the required word and then starts to create a new string builder by individually pulling innerxml of each node until the patterns match. To me this is incredibly convoluted, is there a better way? :confused:
-
Hi, I have a little issue with some code I'm doing. Basically I have a source xml document that looks like this: This is a test line with these:Keyword alphaKeyword Bravo and the problem is this, if I wanted my code to tag a specific word for example replace the word alpha with alpha to create this new node: This is a test line with these:Keyword alphaKeyword Bravo is there a way of identifying a specific point in the overall line to correctly replace it, given that the word alpha could conceivably appear before or after the instance to be tagged? At the moment I am attempting to use a function that pulls the Paragraph Node to text (innertext) and clears out everything after the required word and then starts to create a new string builder by individually pulling innerxml of each node until the patterns match. To me this is incredibly convoluted, is there a better way? :confused:
You could use richTextBox to view the text. Then you can use some of the controls of this richTextBox to get the current position or better the selected text and add your tag in the begginig of the selection and the close tag at the end of the selection, and save it.
-
You could use richTextBox to view the text. Then you can use some of the controls of this richTextBox to get the current position or better the selected text and add your tag in the begginig of the selection and the close tag at the end of the selection, and save it.
Thanks for the reply I appreciate the information. The text is already displayed in a RichTextBox object but without the tags, that is to say the user sees XmlNode.InnerText. Indeed the position of the selection is recorded and used but the problem is that if there are other tags in the root node, or the word is nested then I'm finding it impossible to relate the position from the plain text back to the correct position in the InnerXml
-
Hi, I have a little issue with some code I'm doing. Basically I have a source xml document that looks like this: This is a test line with these:Keyword alphaKeyword Bravo and the problem is this, if I wanted my code to tag a specific word for example replace the word alpha with alpha to create this new node: This is a test line with these:Keyword alphaKeyword Bravo is there a way of identifying a specific point in the overall line to correctly replace it, given that the word alpha could conceivably appear before or after the instance to be tagged? At the moment I am attempting to use a function that pulls the Paragraph Node to text (innertext) and clears out everything after the required word and then starts to create a new string builder by individually pulling innerxml of each node until the patterns match. To me this is incredibly convoluted, is there a better way? :confused:
If you read an XML document into a big honkin' string you can simply use Replace() :-D Or Notepad --Added
Krippers wrote:
To me this is incredibly convoluted, is there a better way?
I'm still trying to figure out what the question is... Are you asking how to find the XmlElements in an XmlDocument that contain certain text? Or how best to make the replacement once you've found one such XmlElement? I wrote the following on the assumption that it is the latter... Provided you loaded the document into an XmlDocument, you can enumerate the Entry elements to find the one(s) you want to affect. I just noodled about and wrote:
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
System.Xml.XmlElement ele ;doc.AppendChild ( ele = doc.CreateElement ( "Test" ) ) ;
ele.InnerText = "Keyword alpha" ;
// At this point we have a Document with an Element whose InnerText is as specified
// And we have a reference to that Element// Split the string
string[] temp = ele.InnerText.Split ( new char[] { ' ' } , 2 ) ;// Remove the current contents of the Element
ele.RemoveAll() ;// Put the first part of the string into the Element as text
ele.AppendChild ( doc.CreateTextNode ( temp [ 0 ] + " " ) ) ;// Wrap the second part of the string in another Element and stick it in
System.Xml.XmlElement nod ;
ele.AppendChild ( nod = doc.CreateElement ( "Emphasis" ) ) ;
nod.InnerText = temp [ 1 ] ;The result:
<Test>Keyword <Emphasis>alpha</Emphasis></Test>
You didn't make the requirements very clear so I chose your simpler example. You may need to use a Regular Expression to split the InnerText. Or perhaps you could use XSLT to perform the change.
modified on Thursday, April 3, 2008 1:28 PM
-
If you read an XML document into a big honkin' string you can simply use Replace() :-D Or Notepad --Added
Krippers wrote:
To me this is incredibly convoluted, is there a better way?
I'm still trying to figure out what the question is... Are you asking how to find the XmlElements in an XmlDocument that contain certain text? Or how best to make the replacement once you've found one such XmlElement? I wrote the following on the assumption that it is the latter... Provided you loaded the document into an XmlDocument, you can enumerate the Entry elements to find the one(s) you want to affect. I just noodled about and wrote:
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
System.Xml.XmlElement ele ;doc.AppendChild ( ele = doc.CreateElement ( "Test" ) ) ;
ele.InnerText = "Keyword alpha" ;
// At this point we have a Document with an Element whose InnerText is as specified
// And we have a reference to that Element// Split the string
string[] temp = ele.InnerText.Split ( new char[] { ' ' } , 2 ) ;// Remove the current contents of the Element
ele.RemoveAll() ;// Put the first part of the string into the Element as text
ele.AppendChild ( doc.CreateTextNode ( temp [ 0 ] + " " ) ) ;// Wrap the second part of the string in another Element and stick it in
System.Xml.XmlElement nod ;
ele.AppendChild ( nod = doc.CreateElement ( "Emphasis" ) ) ;
nod.InnerText = temp [ 1 ] ;The result:
<Test>Keyword <Emphasis>alpha</Emphasis></Test>
You didn't make the requirements very clear so I chose your simpler example. You may need to use a Regular Expression to split the InnerText. Or perhaps you could use XSLT to perform the change.
modified on Thursday, April 3, 2008 1:28 PM
Hi, and thanks very much for the reply I appreciate the effort. That all looks good and I can see it working but I'm probably going to have an issue with multiple instances of the required word in a nested tree. Let me show you what the situation could be: Source Document: <Manual> <Chapter> <Paragraph>In the beginning I had a car</Paragraph> <Paragraph>It was a <Emphasis Type="Bold">fast</Emphasis> car</Paragraph> <Paragraph>The dashboard had: <List><ListItem>Lights</ListItem><ListItem>Dials</ListItem><ListItem>Dashboard</ListItem></List> on the Dashboard</Paragraph> <Paragraph>I like fast cars and I like their dashboards.</Paragraph> </Chapter> </Manual> What the user actually sees is a richTextBox output of each Paragraph element. User view:
In the beginning I had a car It was a fast car The dashboard had: LightsDialsDashboard on the Dashboard I like fast cars and I like their dashboards.
If the user selected the third Dashboard on the third line then would that be an issue to the code you provided? Many thanks for the help. -
Hi, and thanks very much for the reply I appreciate the effort. That all looks good and I can see it working but I'm probably going to have an issue with multiple instances of the required word in a nested tree. Let me show you what the situation could be: Source Document: <Manual> <Chapter> <Paragraph>In the beginning I had a car</Paragraph> <Paragraph>It was a <Emphasis Type="Bold">fast</Emphasis> car</Paragraph> <Paragraph>The dashboard had: <List><ListItem>Lights</ListItem><ListItem>Dials</ListItem><ListItem>Dashboard</ListItem></List> on the Dashboard</Paragraph> <Paragraph>I like fast cars and I like their dashboards.</Paragraph> </Chapter> </Manual> What the user actually sees is a richTextBox output of each Paragraph element. User view:
In the beginning I had a car It was a fast car The dashboard had: LightsDialsDashboard on the Dashboard I like fast cars and I like their dashboards.
If the user selected the third Dashboard on the third line then would that be an issue to the code you provided? Many thanks for the help.I thought we were talking about an XML document, what's with the RichTextBox? Certainly a RichTextBox can take care of itself.