开发者

How to Parse XML in C#

I have managed to r开发者_开发知识库ead in a saved .csv file on my hard disc and get the data to poputlate a data table. I now want to take that data and put it into XML format, then send the XML to a web service. How can I do this?

Thanks for your time.


One option would be to connect to the Web Service using WCF (or ASP.NET Web Services depending on which .NET version you're using). You can then easily fill the proxy classes the framework creates for you and call the service.

The second option would be to use an XmlTextWriter and use that to build the XML document in a MemoryStream. Once you have the XML document built in memory, you can flush the document and send it out to the Web Service.

A third option would be to use LINQ to XML to build the XML document on the fly. Depending on the structure you parse your CSV file into this may be easier/harder for you than using the XmlTextWriter.


XLinq is excellent for putting the data into XML. If you can use an up to date .NET framework I highly recommend it.

There's some info to get started with here (for example) http://www.c-sharpcorner.com/UploadFile/mahesh/xLinkDoc06202007130827PM/xLinkDoc.aspx

As for sending that XML to a web service, perhaps you should be calling the web service via a client automatically generated using the Visual Studio ServiceReference tool.

You may or may not need to send XML to that -- in many cases services are object-based (i.e. you'd not need XLinq, provided you can parse the CSV into the correct objects.)

/EDIT:

Rough example for calling webservices using WCF:

using(var client = new ServiceReference1.ThirdPartyServiceClient())
{
    client.SendSomething("123", "hello");
    string output = client.GetSomething();
    Console.WriteLine(output);
}


Following code can also be used.

public static List<Student> convertXMLtoList(string filePath)
{
XDocument doc = XDocument.Load(filePath);
List<Student> studentsMarks = doc.Descendants("Student").Select(x => new Student()
{
RollNo = int.Parse(x.Element("roll_no").Value),
Name = x.Element("name").Value,
}).ToList();
return studentsMarks;
} 

Where XML looks like

<?xml version="1.0" encoding="utf-8"?>
 <Students>
  <Student>
  <roll_no>1</roll_no>
  <name>XYZ</name>
 </Student>
...
</Students>

You can find more details at http://bit.ly/1eveLz3. you can find CSV to XML also on this post.


I haven't done this with a datatable, but it work great for other objects.

Try something like this:

       public void writeToXML(DataTable inputData, string fileName) {
        XmlSerializer xml = new XmlSerializer(typeof(DataTable));
        StreamWriter sw = new StreamWriter(fileName);
        xml.Serialize(sw, inputData);
        sw.Close();
   }

Edit: Just noticed you needed to pass it along to a web service. So instead of StreamWriter, use a memory stream...but same idea.


There are different options depending on how robust of a solution you want. Assuming you want to follow the quickest route you should look at the XmlWriter. Using an XmlWriter you can quickly generate an XML document, save to string and then pass along to your web service.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜