Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Interesting problem with XML nodes

Interesting problem with XML nodes

Scheduled Pinned Locked Moved C#
helpregexxmltutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Krippers
    wrote on last edited by
    #1

    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:

    P P 2 Replies Last reply
    0
    • K Krippers

      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:

      P Offline
      P Offline
      panoskatws
      wrote on last edited by
      #2

      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.

      K 1 Reply Last reply
      0
      • P panoskatws

        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.

        K Offline
        K Offline
        Krippers
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K Krippers

          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:

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          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

          K 1 Reply Last reply
          0
          • P PIEBALDconsult

            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

            K Offline
            K Offline
            Krippers
            wrote on last edited by
            #5

            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.

            P 1 Reply Last reply
            0
            • K Krippers

              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.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I thought we were talking about an XML document, what's with the RichTextBox? Certainly a RichTextBox can take care of itself.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups