简体   繁体   中英

How to retrieve my xml layout without using XmlDocumen/XmlReader?

I have searched the net to find a solution on retrieving my xml file layout after changing the innertext of some nodes. I'm showing the outcome in a richTextBox. so actually like this: - Retrieve an Xml file from the harddisk with an OpenFileDialog. - show xml in richTextBox - click on button to change some innernodes - After cliking, the innernodes are changed and shown in the richTextBox, thus visible for the user. Up to here it works but the xml file lost it's layout and printed out the text like normal text. Now, by using XmlDocument or XmlReader I will need a string to add as a path. But I don't want to save it to my harddrive directly, I want it to show in my richTextBox. How to do this please? what I have so far:

        //Create a new XmlDocument
        var doc = new XmlDocument();
        //Create a string that points to the file
        string xml = richTextBox1.Text;
        //Load the string to the created XmlDocument.
        doc.LoadXml(xml);

        // Define Default namespace and add prefix to use in Xpath (Mandatory!)
        var nsmgr = new XmlNamespaceManager(doc.NameTable);
        nsmgr.AddNamespace("x", "un:unece:260:data:EEM:02-02-AnXMLTestFile");

        // Selecting All nodes in AnXMLTestFile
        var nodeList = doc.DocumentElement.SelectNodes("//x:AnXMLTestFile", nsmgr);

        XmlNode root = doc.DocumentElement;
        // Loop all parent and childnodes
        foreach (XmlNode node in nodeList)
        {
            // Select Identification node from all nodes
            var identificationNode = node.SelectSingleNode("//x:Identification", nsmgr);
            if (identificationNode != null)
             {
                  // Change value of indentification node to string
                  identificationNode.InnerText = "string";
             }

            // Select SCI node from all parentnodes
            var SCI = node.SelectSingleNode("//x:SCI", nsmgr);
            if (SCI != null)
            {
                // change value of SCI node to string
                SCI.InnerText = "56987465";
            }
        }
        richTextBox1.Text = root.OuterXml;

You can display the xml file in textbox using following code:

doc.Load(@"FILE_PATH");
string outXML = doc.OuterXml;
textBox1.Multiline = true;
textBox1.Text = System.Xml.Linq.XDocument.Parse(outXML).ToString();

I've found the solution. After trying PrettyPrint and BeautyMaker etc... I started to have a burnout on this. I was like: there HAS to be a more simple way!? Because I always had error messages on the streamings in the method. And finally!.... my rescue! I've just added these lines to the code underneath the parts where I change the nodecontents:

>  // Print new XML in richtextbox
>             StringWriter sw = new StringWriter();
>             XmlTextWriter xmltw = new XmlTextWriter(sw);
>             xmltw.Formatting = Formatting.Indented;
>             doc.WriteTo(xmltw);
>             richTextBox1.Text = sw.ToString();

and deleted the line

richTextBox1.Text = root.OuterXml;

It works as a charm for me. ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM