Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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对象和已尝试对象反序列化和LINQ的子对象_C#_Xml_Linq - Fatal编程技术网

C# 无法分析XML对象和已尝试对象反序列化和LINQ的子对象

C# 无法分析XML对象和已尝试对象反序列化和LINQ的子对象,c#,xml,linq,C#,Xml,Linq,我已经实现了两种解析xml树并将其及其子对象转换为对象的方法,但到目前为止都失败了。我尝试了对象序列化作为解释,并使用了Linq作为公认答案的解释 对于第一种方法(反序列化),它一直工作到IndividualOrEntityValidationExtensions的列表属性未分配其值为止(即它保持为空) 使用第二种方法(LINQ),我得到了与整个OutputFilePaths=…块相关的错误 /Users/g-wizkiz/Projects/XmlParser/XmlParser/Program

我已经实现了两种解析xml树并将其及其子对象转换为对象的方法,但到目前为止都失败了。我尝试了对象序列化作为解释,并使用了Linq作为公认答案的解释

对于第一种方法(反序列化),它一直工作到
IndividualOrEntityValidationExtensions
列表
属性未分配其值为止(即它保持为空)

使用第二种方法(LINQ),我得到了与整个
OutputFilePaths=…
块相关的错误

/Users/g-wizkiz/Projects/XmlParser/XmlParser/Program.cs(68,68): Error CS0266: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<XmlParser.models.OutputFilePaths>' to 'XmlParser.models.OutputFilePaths'. An explicit conversion exists (are you missing a cast?) (CS0266) (XmlParser)
/Users/g-wizkiz/Projects/XmlParser/XmlParser/Program.cs(68,68):错误CS0266:无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“XmlParser.models.outputfilepath”。存在显式转换(是否缺少转换?)(CS0266)(XmlParser)
我很乐意选择任何一种方式,尽管我觉得LINQ是一种更优雅的方式

我将展示我的XML和类结构,然后是相应的代码块

XML

<?xml version="1.0" encoding="UTF-8" ?>
<ParameterList>
    <Parameters>
        <NumberOfThreads>10</NumberOfThreads>
        <InputFilePath>C:\Input.dat</InputFilePath>
         <OutputFilePaths>
            <NameFile>name.txt</NameFile>
            <ValidationFile>validation.txt</ValidationFile>
            <AuditLog>audit.txt</AuditLog>
        </OutputFilePaths>
        <DictionaryExtensions>
            <IndividualOrEntityValidationExtensions>
                <ExtensionItem>
                    <Type>dictType1</Type>
                    <Path>dictPath1</Path>
                </ExtensionItem>
                  <ExtensionItem>
                    <Type>dictType2</Type>
                    <Path>dictPat2</Path>
                </ExtensionItem>
            </IndividualOrEntityValidationExtensions>
        </DictionaryExtensions>
    </Parameters>
</ParameterList>

10
C:\Input.dat
name.txt
validation.txt
audit.txt
口述类型1
口述路径1
口述类型2
听写2
课程

[XmlRoot("Parameters")]
    public class Parameters
    {
        public int NumberOfThreads { get; set; }
        public string InputFilePath { get; set; }
        public OutputFilePaths outputFilePaths { get; set; }
        public DictionaryExtensions DictionaryExtensions { get; set; }
    }

  public class OutputFilePaths
    {
        public string NameFile { get; set; }
        public string ValidationFile { get; set; }
        public string AuditLog { get; set; }
    }

        public class DictionaryExtensions
        {
        [XmlElement("IndividualOrEntityValidationExtensions")]
        public IndividualOrEntityValidationExtension IndividualOrEntityValidationExtensions { get; set; }
      }

       public class IndividualOrEntityValidationExtension
        {
            [XmlArrayItem("ExtensionItem")]
            public List<ExtensionItem> ExtensionItem { get; set; }
        }

    public class ExtensionItem
     {
        [XmlAttribute("Type")]
        public string Type { get; set; }
        [XmlAttribute("Path")]
        public string Path { get; set; }
    }
[XmlRoot(“参数”)]
公共类参数
{
public int NumberOfThreads{get;set;}
公共字符串InputFilePath{get;set;}
公共输出文件路径输出文件路径{get;set;}
公共词典扩展词典扩展{get;set;}
}
公共类OutputFilePath
{
公共字符串名称文件{get;set;}
公共字符串验证文件{get;set;}
公共字符串AuditLog{get;set;}
}
公共类字典扩展
{
[XmlElement(“IndividualOrEntityValidationExtensions”)]
public IndividualOrEntityValidationExtension IndividualOrEntityValidationExtensions{get;set;}
}
公共类IndividualOrEntityValidationExtension
{
[XmlArrayItem(“ExtensionItem”)]
公共列表扩展项{get;set;}
}
公共类扩展
{
[XmlAttribute(“类型”)]
公共字符串类型{get;set;}
[XmlAttribute(“路径”)]
公共字符串路径{get;set;}
}
对象反序列化方法

string xmlString = System.IO.File.ReadAllText(@"/Users/g-wizkiz/Projects/XmlParser/XmlParser/parameters.xml");
            XmlSerializer serializer = new XmlSerializer(typeof(List<Parameters>), new XmlRootAttribute("ParameterList"));
            StringReader stringReader = new StringReader(xmlString);
            List<Parameters> parameters = (List<Parameters>)serializer.Deserialize(stringReader)
XDocument doc = XDocument.Parse(xmlString);
            IEnumerable<Parameters> result = from c in doc.Descendants("Parameters")
                                             select new Parameters()
                                             {
                                                 NumberOfThreads = (int)c.Attribute("NumberOfThreads"),
                                                 InputFilePath = (string)c.Attribute("InputFilePath"),
                                                 outputFilePaths = from f in c.Descendants("OutputFilePaths")
                                                                   select new OutputFilePaths()
                                                                   {
                                                                       ValidationFile = (string)f.Attribute("ValidationFile"),
                                                                       AuditLog = (string)f.Attribute("AuditLog"),
                                                                       NameFile = (string)f.Attribute("NameFile")
                                                                   }
                                             };
string xmlString=System.IO.File.ReadAllText(@/Users/g-wizkiz/Projects/XmlParser/XmlParser/parameters.xml);
XmlSerializer serializer=新的XmlSerializer(typeof(List),新的XmlRootAttribute(“ParameterList”);
StringReader StringReader=新的StringReader(xmlString);
列表参数=(列表)序列化程序。反序列化(stringReader)
LINQ方法

string xmlString = System.IO.File.ReadAllText(@"/Users/g-wizkiz/Projects/XmlParser/XmlParser/parameters.xml");
            XmlSerializer serializer = new XmlSerializer(typeof(List<Parameters>), new XmlRootAttribute("ParameterList"));
            StringReader stringReader = new StringReader(xmlString);
            List<Parameters> parameters = (List<Parameters>)serializer.Deserialize(stringReader)
XDocument doc = XDocument.Parse(xmlString);
            IEnumerable<Parameters> result = from c in doc.Descendants("Parameters")
                                             select new Parameters()
                                             {
                                                 NumberOfThreads = (int)c.Attribute("NumberOfThreads"),
                                                 InputFilePath = (string)c.Attribute("InputFilePath"),
                                                 outputFilePaths = from f in c.Descendants("OutputFilePaths")
                                                                   select new OutputFilePaths()
                                                                   {
                                                                       ValidationFile = (string)f.Attribute("ValidationFile"),
                                                                       AuditLog = (string)f.Attribute("AuditLog"),
                                                                       NameFile = (string)f.Attribute("NameFile")
                                                                   }
                                             };
XDocument doc=XDocument.Parse(xmlString);
IEnumerable result=来自文档子体中的c(“参数”)
选择新参数()
{
NumberOfThreads=(int)c.Attribute(“NumberOfThreads”),
InputFilePath=(字符串)c.Attribute(“InputFilePath”),
OutputFilePath=来自c.substands中的f(“OutputFilePath”)
选择新的OutputFilePath()
{
ValidationFile=(字符串)f.Attribute(“ValidationFile”),
AuditLog=(字符串)f.Attribute(“AuditLog”),
NameFile=(字符串)f.Attribute(“NameFile”)
}
};

干杯

我认为问题与
参数
类有关,其中
输出文件路径
是单个实体,而不是
IEnumerable
,或者如果您只需要一个输出文件路径,那么在linq查询中选择
.FirstOrDefault()
(为空值做好准备)。

本地测试,这是唯一的改变:

公共类IndividualOrEntityValidationExtension
{
[XmlElement(“ExtensionItem”)]
公共列表扩展项{get;set;}
}
但是,如果愿意,可以删除层次结构中的某个级别-完全丢弃
IndividualOrEntityValidationExtension
类型:

公共类字典扩展
{
[XmlArray(“IndividualOrEntityValidationExtensions”)]
[XmlArrayItem(“ExtensionItem”)]
公共列表扩展项{get;set;}
}

我修复了你的linq。节点是元素而不是属性

            XDocument doc = XDocument.Parse(xmlString);
            IEnumerable<Parameters> result = (from c in doc.Descendants("Parameters")
                select new Parameters()
                {
                    NumberOfThreads = (int)c.Element("NumberOfThreads"),
                    InputFilePath = (string)c.Element("InputFilePath"),
                    outputFilePaths = (from f in c.Descendants("OutputFilePaths")  
                                    select new OutputFilePaths()
                                    {
                                        ValidationFile = (string)f.Element("ValidationFile"),
                                        AuditLog = (string)f.Element("AuditLog"),
                                        NameFile = (string)f.Element("NameFile")
                                    }).FirstOrDefault()
                }).ToList();
XDocument doc=XDocument.Parse(xmlString);
IEnumerable result=(来自文档子体(“参数”)中的c)
选择新参数()
{
NumberOfThreads=(int)c.Element(“NumberOfThreads”),
InputFilePath=(字符串)c.Element(“InputFilePath”),
OutputFilePath=(从c.subjects(“OutputFilePath”)中的f开始)
选择新的OutputFilePath()
{
ValidationFile=(字符串)f.Element(“ValidationFile”),
AuditLog=(字符串)f.Element(“AuditLog”),