Last post Mar 10, 2016 02:20 PM by Be0
Aug 13, 2007 12:46 AM|williamc|LINK
I tried to read XML file and show the content of XML file in text box with multiple lines. My question is when I assigned value to textbox, it will display
unformat string in textbox, but it will be stored in xml file as correct
Anyone knows why it is, and how to fix it? Thanks
XMLDocument objDoc = new XMLDocument();
XmlNode xn = objDoc.DocumentElements.SelectSigleNode(xpath);
textbox.text = xn.OuterXml;
Aug 13, 2007 01:21 AM|johram|LINK
Given that your TextBox is multiline, you must also make sure you are using the correct font. You need to use a monospace font where all characters are of equal with. Courier is one of the most popular monospace fonts. Good luck!
Aug 13, 2007 01:32 AM|williamc|LINK
Thanks for your Johram, but I think you misunderstood my question. Unformat
doesn't mean font, but indent. For example.
Not Correct: <store><book><name>Harry Potter</name><version>5</version><publish>2007</publish></book></store>
Aug 13, 2007 01:36 AM|TonyMilne|LINK
Regardless of the font your XML text is displayed in, you've lost all indentation... correct?
Instead of assigning xn.OuterXml directly to your Text property - pipe it through the following method to have your XML formatted for you:
protected string FormatXml(XmlNode xmlNode)
StringBuilder bob = new StringBuilder();
// We will use stringWriter to push the formated xml into our StringBuilder bob.
using (StringWriter stringWriter = new StringWriter(bob))
// We will use the Formatting of our xmlTextWriter to provide our indentation.
using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter))
xmlTextWriter.Formatting = Formatting.Indented;
Hope this helps,
Aug 13, 2007 01:54 AM|williamc|LINK
So good, Tony. That's exactly what I want. Thanks, mate.
Here, I got one more question, you might be able to help me. Is it possible to
import xml file in xml element? For example:
<harry> <actor>Daniel Radcliffe</actor> <age>18</age></harry><Hermione> <actress>Emma Watson</actress> <age>17</age></Hermione>
when I read source xml file, it will auto read imported xml file. Is it possible?
Aug 13, 2007 02:53 AM|AlexNetkachov|LINK
This is called XInclude (http://www.w3.org/TR/xinclude/). I'm not sure whether it is implemented in .NET XML, but here is a sample implementation:
http://msdn.microsoft.com/msdnmag/issues/01/01/xml/ (look for "Other Readers" section) and
Nov 24, 2008 10:46 PM|nareshtaduri|LINK
Awesome Bob, it was a great help,
Feb 04, 2009 10:55 AM|nigwoko|LINK
Thanks. Simple and efficient :)
Feb 24, 2011 01:59 PM|postonoh|LINK
Can you give me the code you use to display xml nodes in the text box. I am stuck on this.
I have something like this.
XmlDocument xmldoc = new XmlDocument();
//XmlNodeList xmlnodelist = xmldoc.DocumentElement.ChildNodes;
XmlNode xmlnode = xmldoc;
rows.SubConstructionManager1.Text = xmlnode["BusinessName"].InnerText;
rows.SubContactName1.Text = xmlnode["OwnerName"].InnerText;
rows.SubAddress1.Text = xmlnode["Address"].InnerText;
rows.SubCity1.Text = xmlnode["City"].InnerText;
rows.SubState1.Text = xmlnode["State"].InnerText;
rows.SubZip1.Text = xmlnode["ZipCode"].InnerText;
rows.SubPhone1.Text = xmlnode["Phone"].InnerText;
rows.SubFax1.Text = xmlnode["Fax"].InnerText;
rows.SubEmail1.Text = xmlnode["EmailAddress"].InnerText;
Mar 10, 2016 02:20 PM|Be0|LINK
Tony thanks for this man. Good stuff!