Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi,

Using a:
C#
WebBrowser web = new WebBrowser();

I go to this website: 'http://www.ctt.pt/pdcp/xml_pdcp'
and I want to get the values into strings...
Can you help me?

Regards,
KZ
Posted
Updated 21-May-12 7:18am
v2

C#
XmlDocument xml = new XmlDocument();

            xml.LoadXml(myXmlString); // suppose that myXmlString contains "<Names>...</Names>"

            XmlNodeList xnList = xml.SelectNodes("/Names/Name");
            foreach (XmlNode xn in xnList)
            {
                string firstName = xn["FirstName"].InnerText;
                string lastName = xn["LastName"].InnerText;
                Console.WriteLine("Name: {0} {1}", firstName, lastName);
            }
 
Share this answer
 
Comments
VJ Reddy 21-May-12 13:36pm    
Good answer. 5!
Maciej Los 21-May-12 14:24pm    
Good answer, 5!
A dictionary of values can be created using XElement and LINQ, where the name of the element is the key and the value of element is the value corresponding to the key as shown below:
C#
string xmlData = @"<Erro total=""0"" razao=""PEIN"" inicio=""0"" fim=""0"">
                    <Criterio>
                    <inDistrito/>
                    <inConcelho/>
                    <inLocal/>
                    <inRua/>
                    <inPorta/>
                    <inCodPos/>
                    <inCliente/>
                    <inIdLocal/>
                    <inIdRua/>
                    <inEp/>
                    <inApartado/>
                    <inIdEp/>
                    <inPag>1</inPag>
                    <inMaxPag>20</inMaxPag>
                    <Id_Pesq>68083086</Id_Pesq>
                    </Criterio>
                    </Erro>";

XElement element = XElement.Parse(xmlData);
Dictionary<string,object> values = new Dictionary<string,object>();
element.Element("Criterio").Elements()
    .Select (e => {
        values.Add(e.Name.ToString(),e.Value);
        return e;
    }).Count ();

//[Edit] Modified as pointed out by Losmac [/Edit]

XElement element = XElement.Parse(xmlData);
List<KeyValuePair<string,object>> values = element.Element("Criterio").Elements()
	.Select (e => new KeyValuePair<string,object>(
            e.Name.ToString(),e.Value)
        ).ToList();

//values
//Key             Value
//inDistrito
//inConcelho
//inLocal
//inRua
//inPorta
//inCodPos
//inCliente
//inIdLocal
//inIdRua
//inEp
//inApartado
//inIdEp
//inPag                1
//inMaxPag            20
//Id_Pesq       68083086
 
Share this answer
 
v3
Comments
Maciej Los 21-May-12 13:44pm    
Why Dictionary? If any of above fields is a multi-instances, what could happen? Better way is to use List(of String), especially when we don't know schema (xsd, xslt). Am i right?
Sorry for my language.
VJ Reddy 21-May-12 14:04pm    
You are correct.
But instead of List<string> List<keyvaluepair<string,object>> would be better as each element can store the corresponding value. I have modified the solution accordingly.
Thank you very much for the point :)
Maciej Los 21-May-12 14:23pm    
Now is OK, my 5!
Thank you, VJ ;)
VJ Reddy 21-May-12 14:26pm    
Thank you, losmac :)
C#
DataSet ds = new DataSet("Whatev");

System.IO.TextReader txtReader = new System.IO.StringReader(XmlString);
System.Xml.XmlReader reader = new System.Xml.XmlTextReader(txtReader);
ds.ReadXml(reader);
foreach (System.Data.DataRow DrW in ds.Tables[0].Rows)
{
    foreach (System.Data.DataColumn DC in DrW.Table.Columns)
    {
        Console.Write(DC.ColumnName + "=" + DC.ToString());
    }
}
 
Share this answer
 
v2
Comments
VJ Reddy 21-May-12 14:20pm    
Edit: pre tag for C# code added.
This may help
C#
XmlTextReader reader = new XmlTextReader("http://www.ctt.pt/pdcp/xml_pdcp");
while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element: // The node is an element.
            Console.Write("<" + reader.Name);
            Console.WriteLine(">");
            break;

        case XmlNodeType.Text: //Display the text in each element.
            Console.WriteLine(reader.Value);
            break;

        case XmlNodeType.EndElement: //Display the end of the element.
            Console.Write("</" + reader.Name);
            Console.WriteLine(">");
            break;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900