【问题标题】:How to read custom XML data in C#如何在 C# 中读取自定义 XML 数据
【发布时间】:2016-02-25 11:57:03
【问题描述】:

我正在使用带有 SQL Server 2008 R2 版本的 Visual Studio 2013 Ultimate。

我创建了我的 Web 服务,我从中返回自定义 XML StringBuilder 数据到我的桌面应用程序 (C#)。

所以我的问题是如何存储该 XML 以及如何读取它?

我的自定义 xml 数据演示:

<TableName>students
<row>
<StudentId><![CDATA[1]]></StudentId>
<StudentName><![CDATA[abc]]></StudentName>
<Password><![CDATA[password]]></Password>
</row>
</TableName>

这只是数据的一个例子,像这种格式的数据还有很多。

请有人帮助我如何存储和读取这个 XML!

【问题讨论】:

  • 你想如何存储数据?在数据库中,作为文件?
  • 查看此链接以读取 xml 数据:stackoverflow.com/questions/364253/…。附带说明一下,password 是否经过哈希处理?您只是以明文形式传输它吗?
  • @DanO'Leary 我不想将 xml 存储在数据库中,我只想将它存储在一个变量或任何我可以在 C# 中读取的 xml 阅读器中。
  • 好的,您能分享一下您是如何从桌面应用程序调用 Web 服务的吗?
  • @DanO'Leary 只需通过在我的桌面应用程序中添加 Web 服务的引用来调用该函数,并通过创建 ServiceClient 的对象来调用。即 WebServiceApp.Service1Client 客户端 = new WebServiceApp.Service1Client();并调用函数 client.functionname();

标签: c# xml web-services visual-studio-2013


【解决方案1】:

试试这个 -

public class TableName
{
    public Students students { get; set; }
}

[XmlRoot(ElementName = "students")]
public class Students
{
    [XmlElement(ElementName = "row")]
    public List<Row> Rows { get; set; }
}

public class Row
{
    public string StudentId { get; set; }
    public string StudentName { get; set; }
    public string Password { get; set; }
}

var ser = new XmlSerializer(typeof(TableName));
var def = new XmlSerializerNamespaces();
def.Add("", "");
var obj = ser.Deserialize("xmlPath");

【讨论】:

  • 感谢您的回复,您的解决方案很好,但演示只是为了展示我的数据是如何来的,有一堆数据像这样来,还有更多的行值来。所以对于每个表,行我无法创建类并创建获取设置值,所以我需要不同的解决方案。
猜你喜欢
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多