Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to convert string to xml in C#

2 Answers

0 votes
using System;
using System.Xml;

class Program
{
    static void Main() {
        string str = "<body><head>c# progrmming</head></body>";

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(str);

        XmlNodeList nodelist = xml.GetElementsByTagName("head");

        Console.Write(nodelist[0].InnerXml);
    }
}




/*
run:

c# progrmming

*/

 



answered Aug 27, 2023 by avibootz
0 votes
using System;
using System.Xml;

class Program
{
    static void Main() {
        string str = @"
        <Names>
            <Name>
                <FirstName>Anakin</FirstName>
                <LastName>Skywalker</LastName>
            </Name>
            <Name>
                <FirstName>Albert</FirstName>
                <LastName>Einstein</LastName>
            </Name>
        </Names>";

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(str); 

        XmlNodeList nodelist = xml.SelectNodes("/Names/Name");
        foreach (XmlNode node in nodelist) {
            string firstName = node["FirstName"].InnerText;
            string lastName = node["LastName"].InnerText;
                Console.WriteLine("{0} {1}", firstName, lastName);
        }
    }
}




/*
run:

Anakin Skywalker
Albert Einstein

*/

 



answered Aug 27, 2023 by avibootz

Related questions

1 answer 210 views
1 answer 282 views
1 answer 300 views
300 views asked Aug 7, 2014 by avibootz
1 answer 212 views
1 answer 358 views
...