How do I extract XmlNode.InnerText() values on a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aredz
    New Member
    • Aug 2010
    • 6

    How do I extract XmlNode.InnerText() values on a string?

    Hi,

    I want to extract the data inside an xmlData to a string.

    Let's say I (for example) I have this XML data:

    <text display-inline="yes">This<b>is</b>only<ref>a</ref><i>sample</i></text>

    using XmlNode.InnerTe xt(), how can I achieve this result?

    string[0] = This
    string[1] = <b>is</b>
    string[2] = only
    string[3] = <ref>a</ref>
    string[4] = <i>sample</i>

    I am still in the process studying XML on C# any help would be appreciated.

    Thanks,
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    This is from memory but it should be fairly close...

    There should be an InnerXML property on an XmlNode object. If you call that on a "text" node, it should give you the "This<b>is</b>..." string as is. You can then parse that string however you like to break it up.

    You may be able to parse it as a node structure... I'll take a look and get back to you.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      As it turns out, this is very easy to do with the node structure. I don't normally do this, but it's probably more worthwhile in this case if I just show you the code than if I try to lead you through it.

      Code:
                  string xmlRaw = "<text display-inline=\"yes\">This<b>is</b>only<ref>a</ref><i>sample</i></text>";
      
                  XmlDocument xmlDoc = new XmlDocument();
                  xmlDoc.LoadXml(xmlRaw);
      
                  List<string> items = new List<string>();
      
                  foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                  {
                      switch (node.NodeType)
                      {
                          case XmlNodeType.Text:
                              items.Add(node.Value);
                              break;
                          case XmlNodeType.Element:
                              items.Add(string.Format("<{0}>{1}</{0}>", node.Name, node.InnerText));
                              break;
                      }
                  }
      
                  for (int i = 0; i < items.Count; i++)
                  {
                      Console.WriteLine(string.Format("items[{0}] = {1}", i.ToString(), items[i]));
                  }
      This is obviously making liberal use of assumptions, but you can quite easily detect if a node is a text node, then do this with its children to get the same result.

      Basically, when it gets read into an XmlDocument object, a hierarchical structure is generated and the appropriate node types are setup for you. It's a simple matter of going through the children of a text node and seeing if they are text, or an element, building the appropriate string, and adding it to an array (list, in this case).

      The output of this code is...

      items[0] = This
      items[1] = <b>is</b>
      items[2] = only
      items[3] = <ref>a</ref>
      items[4] = <i>sample</i>

      Comment

      • aredz
        New Member
        • Aug 2010
        • 6

        #4
        Thanks for the reply Gary, I will check on this and let you know if I code this one right.

        One thing, It is XmlNode.InnerXm l(). Sorry for the confusion.

        I will be posting back.

        ----------------

        Thanks for your time on this Gary.

        Mods please close this thread. The question has been answered.

        Thanks!

        aredz~
        Last edited by aredz; Sep 1 '10, 02:10 PM. Reason: changed name of replyee, changed status

        Comment

        Working...