Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.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
C# 使用相同的元素名和不同的属性反序列化XML​;名称_C#_Xml_Linq_Xml Serialization - Fatal编程技术网

C# 使用相同的元素名和不同的属性反序列化XML​;名称

C# 使用相同的元素名和不同的属性反序列化XML​;名称,c#,xml,linq,xml-serialization,C#,Xml,Linq,Xml Serialization,我想在C中反序列化以下XML# 如何使用linq或xml序列化程序反序列化此类xml 目前我正在使用xdocument,获取名为abc的所有节点,并使用if-else梯形图构造对象,但这不是正确的方法 测试是第一个名字 Name是XML中的姓氏 任何帮助都将不胜感激 您描述的映射不是XmlSerializer支持的映射。如果没有以下选项之一,则无法以这种方式序列化Employee: 实现IXmlSerializable——我不建议这样做;这很容易搞得一团糟,尤其是反序列化代码 通过XDocum

我想在C中反序列化以下XML#

如何使用linq或xml序列化程序反序列化此类xml

目前我正在使用xdocument,获取名为abc的所有节点,并使用if-else梯形图构造对象,但这不是正确的方法

测试是第一个名字 Name是XML中的姓氏


任何帮助都将不胜感激

您描述的映射不是
XmlSerializer
支持的映射。如果没有以下选项之一,则无法以这种方式序列化
Employee

  • 实现
    IXmlSerializable
    ——我不建议这样做;这很容易搞得一团糟,尤其是反序列化代码
  • 通过
    XDocument
    XmlDocument
    手动执行(同样,相当多的工作)
  • 创建可从与您的模式匹配的
    XmlSerializer
    使用的DTO模型
坦白地说,我会使用最后一个选项,这可能意味着创建类似(完全未经测试,但我会在一分钟内尝试):

这给了我们(忽略
编码
——这是因为
控制台。Out
):


您描述的映射不是
XmlSerializer
支持的映射。如果没有以下选项之一,则无法以这种方式序列化
Employee

  • 实现
    IXmlSerializable
    ——我不建议这样做;这很容易搞得一团糟,尤其是反序列化代码
  • 通过
    XDocument
    XmlDocument
    手动执行(同样,相当多的工作)
  • 创建可从与您的模式匹配的
    XmlSerializer
    使用的DTO模型
坦白地说,我会使用最后一个选项,这可能意味着创建类似(完全未经测试,但我会在一分钟内尝试):

这给了我们(忽略
编码
——这是因为
控制台。Out
):


使用xdocument而不是序列化将xml解析到类中没有错。这里有一个不使用else梯形图的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication49
{   
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Employee.employees = doc.Descendants("Testdata").Select(x => new Employee()
            {
                FirstName = x.Elements().Where(y => (string)y.Attribute("name") == "fname").Select(y => (string)y).FirstOrDefault(),
                LastName = x.Elements().Where(y => (string)y.Attribute("name") == "lname").Select(y => (string)y).FirstOrDefault()
            }).ToList();

        }
    }
    public class Employee
    {
        public static List<Employee> employees = new List<Employee>();

        public string LastName {get; set;}
        public string FirstName {get;set;}
    }


}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序49
{   
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Employee.employees=doc.subscriptions(“Testdata”)。选择(x=>newemployees()
{
FirstName=x.Elements()。其中(y=>(字符串)y.Attribute(“name”)==“fname”)。选择(y=>(字符串)y)。FirstOrDefault(),
LastName=x.Elements()。其中(y=>(字符串)y.Attribute(“name”)==“lname”)。选择(y=>(字符串)y)。FirstOrDefault()
}).ToList();
}
}
公营雇员
{
public static List employees=new List();
公共字符串LastName{get;set;}
公共字符串名{get;set;}
}
}

使用xdocument而不是序列化将xml解析到类中没有错。这里有一个不使用else梯形图的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication49
{   
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Employee.employees = doc.Descendants("Testdata").Select(x => new Employee()
            {
                FirstName = x.Elements().Where(y => (string)y.Attribute("name") == "fname").Select(y => (string)y).FirstOrDefault(),
                LastName = x.Elements().Where(y => (string)y.Attribute("name") == "lname").Select(y => (string)y).FirstOrDefault()
            }).ToList();

        }
    }
    public class Employee
    {
        public static List<Employee> employees = new List<Employee>();

        public string LastName {get; set;}
        public string FirstName {get;set;}
    }


}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序49
{   
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Employee.employees=doc.subscriptions(“Testdata”)。选择(x=>newemployees()
{
FirstName=x.Elements()。其中(y=>(字符串)y.Attribute(“name”)==“fname”)。选择(y=>(字符串)y)。FirstOrDefault(),
LastName=x.Elements()。其中(y=>(字符串)y.Attribute(“name”)==“lname”)。选择(y=>(字符串)y)。FirstOrDefault()
}).ToList();
}
}
公营雇员
{
public static List employees=new List();
公共字符串LastName{get;set;}
公共字符串名{get;set;}
}
}
使用此处的列表

public class Employee
{
    public static List<Employee> employees = new List<Employee>();

    public Listc<string> Name {get; set;}
}
公共类员工
{
public static List employees=new List();
公共Listc名称{get;set;}
}
使用此处的列表

public class Employee
{
    public static List<Employee> employees = new List<Employee>();

    public Listc<string> Name {get; set;}
}
公共类员工
{
public static List employees=new List();
公共Listc名称{get;set;}
}

我可以检查一下。。。
Test
FirstName
值吗?
Name
LastName
值吗?@marc是的,先生!!我能查一下吗。。。
Test
FirstName
值吗?
Name
LastName
值吗?@marc是的,先生!!不比任何其他xml方法快或慢。只有当您拥有巨大的xml文件时,使用XmlReader才值得。这种情况下的速度更多地与您的xml的样式有关,它对名字和姓氏使用相同的标记。我相信您需要处理空值的大条件?您发布了示例代码,因此我不确定是否需要空值检查。与任何其他xml方法相比,没有更快或更慢的方法。只有当您拥有巨大的xml文件时,使用XmlReader才值得。这种情况下的速度更多地与xml的样式有关,即对名字和姓氏使用相同的标记。我相信您需要处理null的大where条件?您发布了示例代码,因此我不确定是否需要进行null检查。
<?xml version="1.0" encoding="ibm850"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Testdata>
    <abc name="fname">Fred</abc>
    <abc name="lname">Flintstone</abc>
  </Testdata>
</Test>
var obj = (DtoRoot)ser.Deserialize(source);
var emp = new Employee {
    FirstName = obj["fname"],
    LastName = obj["lname"],
};
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication49
{   
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Employee.employees = doc.Descendants("Testdata").Select(x => new Employee()
            {
                FirstName = x.Elements().Where(y => (string)y.Attribute("name") == "fname").Select(y => (string)y).FirstOrDefault(),
                LastName = x.Elements().Where(y => (string)y.Attribute("name") == "lname").Select(y => (string)y).FirstOrDefault()
            }).ToList();

        }
    }
    public class Employee
    {
        public static List<Employee> employees = new List<Employee>();

        public string LastName {get; set;}
        public string FirstName {get;set;}
    }


}
public class Employee
{
    public static List<Employee> employees = new List<Employee>();

    public Listc<string> Name {get; set;}
}