如何在 C# 中解析 XML

发布于 2024-10-02 22:27:44 字数 106 浏览 2 评论 0原文

我已成功读取硬盘上保存的 .csv 文件并获取数据以填充数据表。我现在想要获取该数据并将其转换为 XML 格式,然后将该 XML 发送到 Web 服务。我该怎么做?

感谢您抽出时间。

I have managed to read 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那请放手 2024-10-09 22:27:44

一种选择是使用 WCF(或 ASP.NET Web 服务,具体取决于您使用的 .NET 版本)连接到 Web 服务。然后,您可以轻松填充框架为您创建的代理类并调用该服务。

第二种选择是使用 XmlTextWriter 并使用它在 MemoryStream 中构建 XML 文档。一旦您在内存中构建了 XML 文档,您就可以刷新该文档并将其发送到 Web 服务。

第三种选择是使用 LINQ to XML 构建 XML 文档在飞行中。根据您将 CSV 文件解析为的结构,这对您来说可能比使用 XmlTextWriter 更容易/更困难。

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.

自控 2024-10-09 22:27:44

XLinq 非常适合将数据放入 XML 中。如果您可以使用最新的 .NET 框架,我强烈推荐它。

这里有一些可以开始使用的信息(例如)
http://www.c-sharpcorner.com/UploadFile/mahesh/xLinkDoc06202007130827PM /xLinkDoc.aspx

至于将该 XML 发送到 Web 服务,也许您应该通过使用 Visual Studio ServiceReference 工具自动生成的客户端来调用 Web 服务。

您可能需要也可能不需要向其发送 XML——在许多情况下,服务是基于对象的(即,您不需要 XLinq,前提是您可以将 CSV 解析为正确的对象。)

/编辑:

调用 Web 服务的粗略示例使用WCF:

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

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);
}
遇到 2024-10-09 22:27:44

也可以使用以下代码。

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;
} 

XML 的外观

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

您可以在 http://bit.ly/1eveLz3。您还可以在这篇文章中找到 CSV 到 XML。

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.

木格 2024-10-09 22:27:44

我还没有使用数据表完成此操作,但它对于其他对象非常有用。

尝试这样的操作:

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

编辑:刚刚注意到您需要将其传递给网络服务。因此,不要使用 StreamWriter,而是使用内存流......但想法相同。

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.

別甾虛僞 2024-10-09 22:27:44

根据您想要的解决方案的稳健程度,有不同的选项。假设您想遵循最快的路线,您应该查看 XmlWriter。使用 XmlWriter,您可以快速生成 XML 文档,保存为字符串,然后传递到您的 Web 服务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文