locked
xml node to string convert RRS feed

  • Question

  • hello

     I want to read xml node and convert to string

     

     <TopLevelMenu Name="mnuHelp" Text="&amp;Help">
        <MenuItem Name="About" Text="About" ImageCode="" OnClick="_About" />
        <MenuItem Name="ShortCut" Text="ShortCut" ImageCode="" OnClick="_ShortCut" />
        <MenuItem Name="Calculator" Text="Calculator" ImageCode="" OnClick="_Calculator" />
      </TopLevelMenu>

     

    I have following code  but how to take a  particular node and convert to string

    static string GetXmlString(string strFile)
    {
    	// Load the xml file into XmlDocument object.
    	XmlDocument xmlDoc = new XmlDocument();
    	try
    	{
    		xmlDoc.Load(strFile);
    	}
    	catch (XmlException e)
    	{
    		Console.WriteLine(e.Message);
    	}
    	// Now create StringWriter object to get data from xml document.
    	StringWriter sw = new StringWriter();
    	XmlTextWriter xw = new XmlTextWriter(sw);
    	xmlDoc.WriteTo(xw);
    	return sw.ToString();
    }


    It's Me
    Thursday, January 13, 2011 6:52 AM

Answers

  • You can use simply

    xmlDoc.InnerXml;// this will return string of complete xml doc

    and for child note string you can use

    xmlDoc.ChildNodes[0].InnerXml;

    For getting string of element use InnerXml property

     

    Please mark as helpful if it helps you


    Bimbim.in

    bimbim.in

    • Marked as answer by It_s Meee Thursday, January 13, 2011 8:51 AM
    Thursday, January 13, 2011 7:12 AM
  • Hi again!

    You can load a string with XML to an XMLDocument like so:

    string xmlString;

    XmlDocument doc = new XmlDocument();

    doc.Load(new StringReader(xmlString));

    Please mark the correct answer in this post as answer

    Regards,
    Simon

    • Marked as answer by It_s Meee Thursday, January 13, 2011 8:51 AM
    Thursday, January 13, 2011 7:45 AM

All replies

  • I'm not sure what you want to do. If you want to get the Text from a specific MenuItem node with a given Name try the following:

                foreach (XmlNode node in xmlNode.GetElementsByTagName("MenuItem"))
                {
                    if (node.Attributes["Name"].Value == str)
                    {
                        return node.Attributes["Text"];
                    }
                }

    Regards,
    Simon
    Thursday, January 13, 2011 7:09 AM
  • You can use simply

    xmlDoc.InnerXml;// this will return string of complete xml doc

    and for child note string you can use

    xmlDoc.ChildNodes[0].InnerXml;

    For getting string of element use InnerXml property

     

    Please mark as helpful if it helps you


    Bimbim.in

    bimbim.in

    • Marked as answer by It_s Meee Thursday, January 13, 2011 8:51 AM
    Thursday, January 13, 2011 7:12 AM
  • Hello

    The question is not so clear . but you can iterate through all the nodes in the XMLNodes like this.

     

    // Get Nodes information

     

    XmlNodeList oList = xmlDoc.ChildNodes;

     

    foreach (XmlNode oNode in oList)

    {

     

    Console.WriteLine( oNode.ToString() );

    }

    Hope this helps.

    Thanks,

    Venkataramana M

    Thursday, January 13, 2011 7:12 AM
  • hello

               thanks for reply ,  it 's works

     

    IN my application i have pass xml string on socket and other application recived string 

     

    but how can i load XmlDocument with string with xml data

     

    XmlDocument document = new XmlDocument();
                    document.Load( XmlPath );

                    XmlElement element = document.DocumentElement;

     

     

     

     

     

     


    It's Me
    Thursday, January 13, 2011 7:37 AM
  • Hi again!

    You can load a string with XML to an XMLDocument like so:

    string xmlString;

    XmlDocument doc = new XmlDocument();

    doc.Load(new StringReader(xmlString));

    Please mark the correct answer in this post as answer

    Regards,
    Simon

    • Marked as answer by It_s Meee Thursday, January 13, 2011 8:51 AM
    Thursday, January 13, 2011 7:45 AM
  • Hi It_s Mee,

    You have to use the XMLSerializer class for this.

     

    XmlSerializer xs = new XmlSerializer();

     

    TextWriter objTextWriter = new TextWriter();

    xs.Serialize(objTextWriter, o);

    In .Serialize() method you can pass different type fo writers.

    Using this textwriter you can get the whole xml file in string.

     

    Check this sample code

    public

     

    static string GetSerializedXML(Type t, object o)

    {

     

    XmlSerializer xs = new XmlSerializer(t);

     

    StringBuilder sb = new StringBuilder();

     

    StringWriter output = new StringWriter(sb);

    output.NewLine =

    String.Empty;

    xs.Serialize(output, o);

     

    return output.ToString();

    }

    The above code converts any "Type", class into XML string.

     

    --

    Thanks & Regards

    Gaurav K Arya 

     

    Please mark as answer if it helps

    Thursday, January 13, 2011 8:51 AM