3

My XML could look like this:

<div>
    <p>
       First Text
        <br/>
       Second Text
    </p>
</div>

Loading the xml-file, going through all nodes with the following code:

XmlDocument doc = new XmlDocument();
doc.Load(filepath);

foreach (XmlNode row in doc.SelectNodes("/div/p"))
{
    string subtext = row.InnerText;
    richtextbox.AppendText(subtext + "\n");
}

The result will always look like this:

First TextSecond Text

Now the problem obviously is, that there's no space (or even a line break) between the first & second text. So, is there a way to replace that <br/> with a space/line break?

EgoistDeveloper
  • 775
  • 2
  • 13
  • 37
kyro0
  • 45
  • 3
  • 1
    While I don't know how `XMLDocument.Load` attempts to parse this, HTML is not XML. HTML is a subset of SGML, XML is also a subset of SGML. You should instead use a HTML parser or only load XML. – Cameron Aavik Jun 10 '17 at 01:37
  • You have html not xml. Xml methods will only work on some html files. Use html library instead of an xml library. – jdweng Jun 10 '17 at 01:38
  • 1
    @CameronAavik @jdweng - if it was HTML the br would be `
    ` and not `
    ` (self closing). As it stands that is well formed XML.
    – Daniel Haley Jun 10 '17 at 03:38

1 Answers1

0

You can use the following XPath:

doc.SelectNodes("/div/p/text()")

It gives you two text nodes before and after br tag.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • This sadly doesn't work in my case because as you said, it creates two text nodes. I need that node as a whole, without splitting it up. – kyro0 Jun 10 '17 at 22:46