Click to See Complete Forum and Search --> : read and write XML


tis707
December 9th, 2008, 11:17 PM
Hi all i have xml file need to change that with diffrent languages

<?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
<?xml version="1.0" encoding="utf-8" standalone="yes" ?><WebEditorDocument><Template Name="Test"/><Data><Paragraph Format="Text">Diagrammet avser rensbar böj med renslucka samt med samma dimension på stos och huvudrör (Ød<Subscript>3</Subscript> = Ød<Subscript>1</Subscript>).</Paragraph><Paragraph Format="Text2">För renslucka samt Ød<Subscript>3</Subscript> = Ød<Subscript>1</Subscript>, är tryckfallet ca 30% <Break/>lägre.</Paragraph><Paragraph Format="Text">För renslucka samt ett stegs mindre stosdimension (Ød<Subscript>3</Subscript> &lt; Ød<Subscript>1</Subscript>) är tryckfallet ca 30% lägre.</Paragraph><Paragraph Format="Text">För renslucka samt ett stegs mindre stosdimension (Ød<Subscript>3</Subscript> &lt; Ød<Subscript>1</Subscript>) är tryckfallet ca 50% lägre.</Paragraph></Data></WebEditorDocument>

i tried to read the file

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

dannystommen
December 10th, 2008, 02:46 AM
I would suggest to use xPath, you can retrieve an node much easier and you don't need the switch statment


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.?

tis707
December 10th, 2008, 04:18 AM
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

dannystommen
December 10th, 2008, 06:33 AM
if you use my code, the subscript node can be found in the p.Chilnodes properties

tis707
December 10th, 2008, 07:54 AM
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