Hello, I have a file I saved as an XML file and want different parts of the file to display in two text boxes. What is happening is the whole content of the file displays in a textbox as:

<?xml version="1.0" encoding="utf-16"?>
<text>
h
hkj
</text>

...in both text boxes but I want individual parts to go into each file.

This is the content inside the file:

<test>
//gibberish text :)
h
hkj
</test>

And this is the code for the XML:

StringBuilder output = new StringBuilder();



            // Create an XmlReader
            using (XmlTextReader xmlString = new XmlTextReader("C:\\fileLocation.xml"))
            {
                XmlWriterSettings ws = new XmlWriterSettings();
                ws.Indent = true;
                using (XmlWriter writer = XmlWriter.Create(output, ws))
                {

                    // Parse the file and display each of the nodes.
                    while (xmlString.Read())
                    {
                        switch (xmlString.NodeType)
                        {
                            case XmlNodeType.Element:
                                writer.WriteStartElement(xmlString.Name);
                                break;
                            case XmlNodeType.Text:
                                writer.WriteString(xmlString.Value);
                                break;
                            case XmlNodeType.XmlDeclaration:
                            case XmlNodeType.ProcessingInstruction:
                                writer.WriteProcessingInstruction(xmlString.Name, xmlString.Value);
                                break;
                            case XmlNodeType.Comment:
                                writer.WriteComment(xmlString.Value);
                                break;
                            case XmlNodeType.EndElement:
                                writer.WriteFullEndElement();
                                break;
                        }
                    }

                }
            }
            textBox1.Text = output.ToString();
            textBox2.Text = output.ToString();

        }

Recommended Answers

All 2 Replies

xml mostly used for database

xml mostly used for database

Loads of help . . .

Xml is used to store semi-structured data, mostly used for databases as it allows you to transefer data across database formats but i.e. SQL Server to MS Access.

this worked for me when i did my 3rd year project for uni, it reads two nodes from the xml document, stores them in variables and puts them in the textboxes, you will need to put you data in labelled nodes for it to work and for organisation too.

hope this helps.

int objectTypeID = 0;
            int stateTypeID = 0;

            try
            {
                // Initialises instance of XmlDocument class called config
                XmlDocument config = new XmlDocument();
                // Loads existing Xml document, located at directory this is stored in path and stores it into config
                config.Load("C:\\fileLocation.xml");

                // Creats list of all nodes within the xml document with tag of ObjectTypeID
                XmlNodeList ObjectTypeID = config.GetElementsByTagName("ObjectTypeID");

                //Loops through for each element in the list
                foreach (XmlNode n in ObjectTypeID)
                {
                    //Converts XmlNode to XmlElement
                    XmlElement _ObjectTypeID = (XmlElement)n;
                    //Converts and stroes value of specified attribute in property of ObjectTypeState class
                    stateTypeID = Convert.ToInt32(_ObjectTypeID.GetAttribute("ID"));
                }

                textBox1.Text = "ID1 = " + objectTypeID;

                XmlNodeList StateType = config.GetElementsByTagName("StateType");

                foreach (XmlNode n in StateType)
                {
                    XmlElement _statetype = (XmlElement)n;
                    stateTypeID = Convert.ToInt32(_statetype.GetAttribute("ID"));
                }

                textBox2.Text = "ID2 = " + stateTypeID;

            }
            catch
            {
                throw (new Exception("asdfghjk"));
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.