Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将XML解析为需要返回给客户端的C#对象_C#_Xml_Xml Parsing_Xsd_Xml Serialization - Fatal编程技术网

将XML解析为需要返回给客户端的C#对象

将XML解析为需要返回给客户端的C#对象,c#,xml,xml-parsing,xsd,xml-serialization,C#,Xml,Xml Parsing,Xsd,Xml Serialization,如何将这个XML文档解析为C#对象,以便以JSON或XML的形式返回该对象? 请帮忙 <?xml version="1.0" encoding="UTF-8"?> <token_attributes> <token> <name>first_name</name> <type>string</type> <display>First Name</display>

如何将这个XML文档解析为C#对象,以便以JSON或XML的形式返回该对象? 请帮忙

 <?xml version="1.0" encoding="UTF-8"?>
<token_attributes>
  <token>
    <name>first_name</name>
    <type>string</type>
    <display>First Name</display>
    <description>First Name of Employee</description>
  </token>
  <token>
    <name>last_name</name>
    <type>string</type>
    <display>Last Name</display>
    <description>Last Name of Employee</description>
  </token>
  <token>
    <name>Email</name>
    <type>string</type>
    <display>Customer Email</display>
    <description>The Email to contact customer</description>
  </token>
  <token>
    <name>received_date</name>
    <type>date</type>
    <display>Received Date</display>
    <description>Received Date</description>
  </token>
  <token>
    <name>phone_number</name>
    <type>string</type>
    <display>Customer Support Phone Number</display>
    <description>The phone number to contact customer support</description>
  </token>
  <token>
    <name>employer_name</name>
    <type>string</type>
    <display>Employer Name</display>
    <description>The name of the Employer</description>
  </token>
  <token>
    <name>employee_id</name>
    <type>number</type>
    <display>Employee ID</display>
    <description>The Id of an Employee</description>
  </token>
  <token>
    <name>URL</name>
    <type>string</type>
    <display>Employer URL</display>
    <description>Employer URL</description>
  </token>
  <token>
    <name>claim_information</name>
    <type>array</type>
    <display>Claim Details</display>
    <description>Claim Information</description>
    <tokens>
        <token>
            <name>claim_id</name>
            <type>number</type>
            <display>Claim ID</display>
            <description>The ID of the Claim</description>
        </token>
        <token>
            <name>amount_claimed</name>
            <type>number</type>
            <display>Claim amount</display>
            <description>The claim amount</description>
        </token>
        <token>
            <name>expense_name</name>
            <type>string</type>
            <display>Expense Name</display>
            <description>The name of the expenses</description>
        </token>
        <token>
            <name>claim_date</name>
            <type>date</type>
            <display>Claim Date</display>
            <description>The date that the claim occurred</description>
        </token>
         <token>
            <name>claim_status</name>
            <type>string</type>
            <display>Claim Status</display>
            <description>The status of the claim</description>
        </token>
         <token>
            <name>claim_status_reason</name>
            <type>string</type>
            <display>Claim Status Reason</display>
            <description>The reason for the claim status</description>
        </token>
      </tokens>
    </token>
  </token_attributes>

名字
一串
名字
雇员姓名
姓
一串
姓
雇员的姓氏
电子邮件
一串
客户电子邮件
联系客户的电子邮件
收到日期
日期
收到日期
收到日期
电话号码
一串
客户支持电话号码
联系客户支持的电话号码
雇主名称
一串
雇主名称
雇主的姓名
雇员身份证
数
员工ID
雇员的身份证
统一资源定位地址
一串
雇主网址
雇主网址
索赔信息
排列
索赔详情
索赔信息
索赔id
数
索赔ID
索赔的ID
索赔额
数
索赔额
索赔额
费用名称
一串
费用名称
费用的名称
索赔日期
日期
索赔日期
索赔发生的日期
索赔身份
一串
索赔状态
索赔的状况
索赔(状态)(原因)
一串
索赔状态原因
索赔状态的原因
我创建了C#对象,如下所示:

public class TokenDetails
{

    public string Name { get; set; }

    public string Type { get; set; }

    public string Display { get; set; }

    public string Description { get; set; }

}


public class Tokens
{

    public string Name { get; set; }

    public string Type { get; set; }

    public string Display { get; set; }

    public string Description { get; set; }

    public List<TokenDetails> TokenDetailsRepeater { get; set; }


}
公共类令牌详细信息
{
公共字符串名称{get;set;}
公共字符串类型{get;set;}
公共字符串显示{get;set;}
公共字符串说明{get;set;}
}
公共类令牌
{
公共字符串名称{get;set;}
公共字符串类型{get;set;}
公共字符串显示{get;set;}
公共字符串说明{get;set;}
公共列表令牌详细信息重复程序{get;set;}
}
到目前为止,这就是我所拥有的:

    XmlDocument xml = new XmlDocument();
    xml.LoadXml(myXMLString); //suppose that myXMLString contains the above XML

    List<Tokens> templateTokens = new List<Tokens>();

    XmlNodeList xnList = xml.SelectNodes("/token_attributes/token");
    foreach (XmlNode xn in xnList)
    {
        string name = xn["name"].InnerText;
        string type = xn["type"].InnerText;
        string display = xn["display"].InnerText;
        string description = xn["description"].InnerText;

        Tokens templateToken = new Tokens();

        templateToken.Name = name;
        templateToken.Type = type;
        templateToken.Display = display;
        templateToken.Description = description;

        templateToken.TokenDetailsRepeater = new List<TokenDetails>();

        ....
        ....
        ....
        ....
        //This is where I am stuck.. I don't know how to read the child node Tokens
        //Is this the right way to parse the XML? Or is there any other easier way?
        //Also it is possible that the child node Tokens may or may not exist.
        //So how do I check for any null string or element? 
        // Please help... Thanks in advance.


        templateTokens.Add(templateToken);
      }
xmldocumentxml=newxmldocument();
LoadXml(myXMLString)//假设myXMLString包含上述XML
List templateTokens=新列表();
XmlNodeList xnList=xml.SelectNodes(“/token\u attributes/token”);
foreach(xnList中的XmlNode xn)
{
字符串名称=xn[“名称”]。InnerText;
字符串类型=xn[“类型”]。InnerText;
字符串显示=xn[“显示”]。InnerText;
字符串描述=xn[“描述”]。InnerText;
Tokens templateToken=新令牌();
templateToken.Name=Name;
templateToken.Type=Type;
templateToken.Display=Display;
templateToken.Description=描述;
templateToken.TokenDetailsRepeater=新列表();
....
....
....
....
//这就是我被卡住的地方。我不知道如何读取子节点令牌
//这是解析XML的正确方法吗?还是有其他更简单的方法?
//子节点令牌也可能存在,也可能不存在。
//那么如何检查任何空字符串或元素呢?
//请帮忙…提前谢谢。
添加(templateToken);
}
我不知道如何读取子节点令牌 还不确定这是否是解析上述XML的正确方法? 请帮忙。。。
提前谢谢。

只需声明此类即可

public class token
{
    public string name { get; set; }
    public string type { get; set; }
    public string display { get; set; }
    public string description { get; set; }
    public List<token> tokens { get; set; }
}
公共类令牌
{
公共字符串名称{get;set;}
公共字符串类型{get;set;}
公共字符串显示{get;set;}
公共字符串说明{get;set;}
公共列表标记{get;set;}
}
然后反序列化为

XmlSerializer ser = new XmlSerializer(typeof(List<token>),new XmlRootAttribute("token_attributes"));
var tokens = (List<token>)ser.Deserialize(File.OpenRead(filename));
XmlSerializer ser=newxmlserializer(typeof(List),newxmlrootattribute(“token_属性”);
var tokens=(List)ser.Deserialize(File.OpenRead(filename));

@Dirty,我编写代码并测试它只是为了回答您的问题,但作为一个非常忙碌的人,您在测试之前发表评论。谢谢EZI。我理解。我会试试看。谢谢,我没有存档。我有一个字符串中的xml。那么如何将File.OpenRead(filename))更改为?@Ditty
var tokens=(List)ser.Deserialize(新的StringReader(“yourxmlstring”)