CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: read and write XML

  1. #1
    Join Date
    Jan 2005
    Posts
    224

    read and write XML

    Hi all i have xml file need to change that with diffrent languages
    Code:
    <?xml version="1.0" encoding="utf-8" standalone="yes" ?><WebEditorDocument><Template Name="Test"/><Data><Paragraph Format="Text">The graph applies to a cleaning bend with cleaning cap and with the same dimension of take-off and main duct (d<Subscript>3</Subscript>=d<Subscript>1</Subscript>).</Paragraph><Paragraph Format="Text2">The pressure drop is about 30% lower for cleaning cap and d<Subscript>3</Subscript>=d<Subscript>1</Subscript>.</Paragraph><Paragraph Format="Text2">The pressure drop is about 30% lower for cleaning cap and one step smaller take-off dimension (d<Subscript>3 </Subscript>&lt; d<Subscript>1</Subscript>).</Paragraph><Paragraph Format="Text2">The pressure drop is about 50% lower for cleaning cap KCU and one step smaller take-off dimension (d<Subscript>3 </Subscript>&lt; d<Subscript>1</Subscript>).</Paragraph></Data></WebEditorDocument>
    And after the translation the value in the text file should be
    Code:
     <?xml version="1.0" encoding="utf-8" standalone="yes" ?><WebEditorDocument><Template Name="Test"/><Data><Paragraph Format="Text">Diagrammet avser rensbar bj med renslucka samt med samma dimension p stos och huvudrr (d<Subscript>3</Subscript> = d<Subscript>1</Subscript>).</Paragraph><Paragraph Format="Text2">Fr renslucka samt d<Subscript>3</Subscript> = d<Subscript>1</Subscript>, r tryckfallet ca 30% <Break/>lgre.</Paragraph><Paragraph Format="Text">Fr renslucka samt ett stegs mindre stosdimension (d<Subscript>3</Subscript> &lt; d<Subscript>1</Subscript>) r tryckfallet ca 30% lgre.</Paragraph><Paragraph Format="Text">Fr renslucka samt ett stegs mindre stosdimension (d<Subscript>3</Subscript> &lt; d<Subscript>1</Subscript>) r tryckfallet ca 50% lgre.</Paragraph></Data></WebEditorDocument>
    i tried to read the file

    Code:
     try
                    {
                        StringReader srXmlValue = new StringReader(param.ParamValue.Replace("<Break/>", Environment.NewLine));
                        XmlTextReader trXmlValue = new XmlTextReader(srXmlValue);
                        while (trXmlValue.Read())
                        {
                            switch (trXmlValue.NodeType)
                            {
                                case XmlNodeType.Element: // The node is an element. 
                                    if (trXmlValue.Name.Trim().ToLower() == "paragraph")
                                    {
                                        txtParamValue.Text += String.Format("[###Paragraph-{0}###]", trXmlValue["Format"]);
                                        txtParamValue.Text += Environment.NewLine;
                                    }
                                    break;
                                case XmlNodeType.Text: //Display the text in each element. 
                                    txtParamValue.Text += Environment.NewLine;
                                    txtParamValue.Text += trXmlValue.Value;
                               //     txtParamValue.Text += Environment.NewLine;
                                //    txtParamValue.Text += Environment.NewLine;
                                    break;
                            }
                        }
                    }
                    catch
                    {
                        txtParamValue.Text = param.ParamValue;
                    }
    but getting problem near <subscript>3</subscript>,
    i have to show the entire text in paragraph tag in richtextbox

    thanks

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: read and write XML

    I would suggest to use xPath, you can retrieve an node much easier and you don't need the switch statment

    Code:
             string richtextbox = "";
    
             XmlDocument xmlDoc = new XmlDocument();
             xmlDoc.Load("XMLFile1.xml");
    
             XmlNodeList paragraphs = xmlDoc.SelectNodes("WebEditorDocument/Data/Paragraph");
    
             foreach (XmlNode p in paragraphs) {
                richtextbox += string.Format("paragraph-{0}", p.Attributes["Format"]);
                richtextbox += Environment.NewLine;            
                
                //and do the rest of your processing using this xmlNode
             }
    And second, what exactly are you trying to achieve with the subscript node.?

  3. #3
    Join Date
    Jan 2005
    Posts
    224

    Re: read and write XML

    Thanks for the reply
    Actuall i have to read a text file in that i have this XML for
    like this
    BBC|2045|132| here the xml text

    i have to show the xml text in plain text and after modificatations again rewrite to text file

    in the xml i have to show the entire text in paragraph node where i am unable to get this subscript tag

    Thanks a lot

  4. #4
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: read and write XML

    if you use my code, the subscript node can be found in the p.Chilnodes properties

  5. #5
    Join Date
    Jan 2005
    Posts
    224

    Re: read and write XML

    Hi dannystommen,

    Thanks for the reply
    i am going in the as specified by you,i am geting complete text by using p.innertext

    and after changing the text how can i follow to write it into XML format


    Thanks a lot
    Last edited by tis707; December 10th, 2008 at 09:15 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)