Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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_Serialization_Deserialization_Linq To Xml - Fatal编程技术网

C# 如何序列化xml并获取最深的节点

C# 如何序列化xml并获取最深的节点,c#,xml,serialization,deserialization,linq-to-xml,C#,Xml,Serialization,Deserialization,Linq To Xml,我有以下xml文件 <?xml version="1.0" encoding="utf-8"?> <root> <file_1> <file_name Value="" /> <date Value="" /> <information> <page1> <percentage Value="90

我有以下xml文件

<?xml version="1.0" encoding="utf-8"?>
<root>
    <file_1>
        <file_name Value="" />
        <date Value="" />
        <information>
            <page1>
                <percentage Value="90%" />                
                <profit Value="50%" />                
                <total Value="$1500" />                
            </page1>
        </information>
    </file_1>
</root>

我想序列化该xml,但我希望page1节点中的所有子节点都可以像属性一样处理,例如:

var xmlInfo = new List<xmlClass>();
var FieldName = xmlInfo[0].FieldName; // the value of FieldName should be percentage
var data = xmlInfo[0].Value; // the value of data should be 90%
var xmlInfo=new List();
var FieldName=xmlInfo[0]。FieldName;//FieldName的值应为percentage
var data=xmlInfo[0]。值;//数据值应为90%
换句话说,我只对最深的节点感兴趣,以便将它们序列化为对象

我有一个序列化方法,但我不知道如何构建该类

public static T Deserialize<T>(XDocument doc)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (var reader = doc.Root.CreateReader())
            {
                return (T)xmlSerializer.Deserialize(reader);
            }
        }
publicstatict反序列化(xdocumentdoc)
{
XmlSerializer XmlSerializer=新的XmlSerializer(typeof(T));
使用(var reader=doc.Root.CreateReader())
{
返回(T)xmlSerializer.Deserialize(读取器);
}
}

将xml linq与字典一起使用:

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

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

            Dictionary<string, string> dict = doc.Descendants("page1")
                .First()
                .Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y.Attribute("Value"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Dictionary dict=文档子体(“第1页”)
.First()
.要素()
.GroupBy(x=>x.Name.LocalName,y=>(字符串)y.Attribute(“值”))
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
}
}
}

XPath表达式
/*[not(child::node())]
返回没有子节点的所有元素(子元素或子文本节点),如果这与您对“deep”的定义相匹配的话。

是什么让XML“特别”?什么是
xmlClass
?我只是更改了标题?您能更具体地描述一下“最深的节点”吗?您不想让它从“文件名”和“日期”中获取值吗?