Jump to content

Xml Help In C# (Dot Net Guys)


Recommended Posts

Posted

Hi Guys,

Need help in getting results from the xml tag.

I have the below xml formatted data. How can i extract the values of Name, Description, Created, FileName values from the below xml structure to an object. 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:dataset xmlns:ns1="http://www.mysol.com/entity">
<ns1:Report Name="SalesOctober08" Description="Monthly Sales Analysis Rpt 1" Created="01/01/2008" FileName="Sales_rpt_2008" />
<ns1:Report Name="SalesOctober09" Description="Monthly Sales Analysis Rpt 2" Created="01/01/2009" FileName="Sales_rpt_2009" />

</ns1:dataset>

 

Thanks.

Posted

Open XML file with MS Excel man...

Posted

Open XML file with MS Excel man...

i need programmatially....

i need to extract the values and tie it to an object

Posted

i need programmatially....

i need to extract the values and tie it to an object

 

check that link..

Posted

System.Xml.XmlDocument myxmldoc = newSystem.Xml.XmlDocument();
myxmldoc.Load("MyXMLFile1.xml");//if you have the xmlfile outside the solution explorer insert the full path to it

 

System.Xml.XmlNodeList mynodes = myxmldoc.SelectNodes("dataset/add what ever attributes u need");

foreach (System.Xml.XmlNode mynode in mynodes)
{
MessageBox.Show(mynode.InnerText);
}

  • Upvote 1
Posted

Check XElement and XDocument tutorials in dotnetperls etc... It's very Easy to extract attributes and tag values

Posted

System.Xml.XmlDocument myxmldoc = newSystem.Xml.XmlDocument();
myxmldoc.Load("MyXMLFile1.xml");//if you have the xmlfile outside the solution explorer insert the full path to it

System.Xml.XmlNodeList mynodes = myxmldoc.SelectNodes("[font='Segoe UI']dataset[/font]/add what ever attributes u need");
foreach (System.Xml.XmlNode mynode in mynodes)
{
MessageBox.Show(mynode.InnerText);
}


Thanks....

What if I the XML content in xelement.... .?

I tried this it had null...
Should I ignore ns1???
Posted

Check XElement and XDocument tutorials in dotnetperls etc... It's very Easy to extract attributes and tag values


I have done it before for XML message of type
<Name> john<\Name>

But not of type <person><name = "john"<\person>

Anyways I will check the sites
Posted

I have done it before for XML message of type
<Name> john<\Name>

But not of type <person><name = "john"<\person>

Anyways I will check the sites

 

even to get John its very easy with XElement man ... check it out

Posted

</person>

xmlelement ki parse method untadi use that and iterate through name.descedants aa name check chesko ganthe :5_2_108:

I have done it before for XML message of type
<Name> john<\Name>

But not of type <person><name = "john"<\person>

Anyways I will check the sites

 

Posted

thanks for your help guys...

I found the solution...

 

Since my request had "ns1:" in the beginning of the xml tag.....i had to convert to string and then replcase all "ns1:" with blanks

and then used parse ....

 

 

string frmTReq = "<ns1:dataset xmlns:ns1="http://www.mysol.com/entity">

<ns1:Report Name="SalesOctober08" Description="Monthly Sales Analysis Rpt 1" Created="01/01/2008" FileName="Sales_rpt_2008" />
<ns1:Report Name="SalesOctober09" Description="Monthly Sales Analysis Rpt 2" Created="01/01/2009" FileName="Sales_rpt_2009" />

</ns1:dataset>";

List<Person> personList = new list<Person>();

XDocument xDoc = XDocument.Parse(frmTReq);
 
            foreach (var node in xDoc.Descendants("Report"))
            {
                Person pr = new Person() 
                {
                    Name= node.Attribute("Name").Value,
                    Description= node.Attribute("Description").Value,
                    Created= node.Attribute("Created").Value,
                    FileName= node.Attribute("FileName").Value
                };
               personlist.add(pr);
            }
 
This helped my scneriao...
 
thanks guys. 
×
×
  • Create New...