Need help with XML
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Need help with XML

  1. #1
    Join Date
    Sep 2011
    Posts
    69

    Question Need help with XML

    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:
    Code:
        <?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:

    Code:
        <test>
        //gibberish text :)
        h
        hkj
        </test>
    And this is the code for the XML:

    Code:
        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();
         
        }

  2. #2
    Join Date
    Oct 2011
    Posts
    2

    Re: Need help with XML

    OK, maybe I am just not fully understanding why you are doing it this way, but there are multiple other ways to accomplish this. If you are just taking an elements value and putting it into a text box then something like this shoudl be all you need...

    Code:
    protectedvoid HandleXMLText()
    {
      string sLang = getLang();
      var xmlFile = Server.MapPath( string.Format("/xml/{0}/HA_survey.xml", sLang) );
      if (!System.IO.File.Exists(xmlFile))
      {
         thrownew System.IO.FileNotFoundException(
            "XML translation file not found in method HandleXMLText()", xmlFile);
      }
      XmlDocument doc = newXmlDocument();
      doc.Load(xmlFile);
      XmlNode root = doc.DocumentElement;
      Int32 currentSurveyPage = this.HASurveySessionPage; //get page
      // Literal Language text from xml
      // General Page text
      litHealthEvalTitle.Text = root.SelectSingleNode("litHealthEvalTitle").ChildNodes[0].Value;
      litProfInfo.Text = root.SelectSingleNode("litProfInfo").ChildNodes[0].Value;
    
    }
    substitute the litteral.Text to your textbox.Text

    But also you would probably want better segmentation of the data you are storing in the XML similar to this for my example to work:
    Code:
    <?xmlversion="1.0"encoding="utf-8"?>
      <article>
         <litHealthEvalTitle><![CDATA[Health Evaluation:]]></litHealthEvalTitle>
         <litProfInfo><![CDATA[Profile Information]]></litProfInfo>
    </article>
    



    Now if you are looking for mor dynamic stuff you should let us know about that, but simple xml value to mapping can be done like above.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center