Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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文档中存在错误(0,0)_C#_Xml_Linq - Fatal编程技术网

C#反序列化-XML文档中存在错误(0,0)

C#反序列化-XML文档中存在错误(0,0),c#,xml,linq,C#,Xml,Linq,我正在尝试反序列化XDocumnet,并收到错误:“XML文档网(0,0)中存在错误。” XML: 我在想,也许我可以跳过整个反序列化过程,在创建新XDoc时使用这些类。测试调用: XmlSerializer serializer = new XmlSerializer(typeof(Machine)); using (System.IO.FileStream stream = new System.IO.FileStream(@"C:\Users\Administrator\Desktop\t

我正在尝试反序列化XDocumnet,并收到错误:“XML文档网(0,0)中存在错误。”

XML:

我在想,也许我可以跳过整个反序列化过程,在创建新XDoc时使用这些类。

测试调用:

XmlSerializer serializer = new XmlSerializer(typeof(Machine));
using (System.IO.FileStream stream = new System.IO.FileStream(@"C:\Users\Administrator\Desktop\test.xml", System.IO.FileMode.Open))
{
     Machine machine = (Machine)serializer.Deserialize(stream);
     // do stuff with machine
}
xml文档的类

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Machine
{        /// <remarks/>
    public MachineAsset Asset { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAsset
{
    public string Product { get; set; }

    public uint OrderNumber { get; set; }

    public uint ServiceTag { get; set; }

    public System.DateTime ShipDate { get; set; }

    [System.Xml.Serialization.XmlArrayItemAttribute("Warranty", IsNullable = false)]
    public MachineAssetWarranty[] Warranties { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAssetWarranty
{
    public MachineAssetWarrantyService Service { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Services { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAssetWarrantyService
{
    public string ServiceDescription { get; set; }

    public string Provider { get; set; }

    public System.DateTime StartDate { get; set; }

    public System.DateTime EndDate { get; set; }

    public string Type { get; set; }
}
//
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类机器
{        /// 
公共机器集资产{get;set;}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类机器集
{
公共字符串乘积{get;set;}
公共uint订单号{get;set;}
公共uint服务标签{get;set;}
public System.DateTime ShipDate{get;set;}
[System.Xml.Serialization.XmlArrayItemAttribute(“担保”,IsNullable=false)]
公共机器设置保修[]保修{get;set;}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类机器设置保修
{
公共机器设置保证服务服务{get;set;}
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串服务{get;set;}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类机器设置保修服务
{
公共字符串ServiceDescription{get;set;}
公共字符串提供程序{get;set;}
public System.DateTime开始日期{get;set;}
public System.DateTime EndDate{get;set;}
公共字符串类型{get;set;}
}
如果您仍然遇到奇怪的行为,请发布内部异常测试呼叫:

XmlSerializer serializer = new XmlSerializer(typeof(Machine));
using (System.IO.FileStream stream = new System.IO.FileStream(@"C:\Users\Administrator\Desktop\test.xml", System.IO.FileMode.Open))
{
     Machine machine = (Machine)serializer.Deserialize(stream);
     // do stuff with machine
}
xml文档的类

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Machine
{        /// <remarks/>
    public MachineAsset Asset { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAsset
{
    public string Product { get; set; }

    public uint OrderNumber { get; set; }

    public uint ServiceTag { get; set; }

    public System.DateTime ShipDate { get; set; }

    [System.Xml.Serialization.XmlArrayItemAttribute("Warranty", IsNullable = false)]
    public MachineAssetWarranty[] Warranties { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAssetWarranty
{
    public MachineAssetWarrantyService Service { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Services { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAssetWarrantyService
{
    public string ServiceDescription { get; set; }

    public string Provider { get; set; }

    public System.DateTime StartDate { get; set; }

    public System.DateTime EndDate { get; set; }

    public string Type { get; set; }
}
//
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类机器
{        /// 
公共机器集资产{get;set;}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类机器集
{
公共字符串乘积{get;set;}
公共uint订单号{get;set;}
公共uint服务标签{get;set;}
public System.DateTime ShipDate{get;set;}
[System.Xml.Serialization.XmlArrayItemAttribute(“担保”,IsNullable=false)]
公共机器设置保修[]保修{get;set;}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类机器设置保修
{
公共机器设置保证服务服务{get;set;}
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串服务{get;set;}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类机器设置保修服务
{
公共字符串ServiceDescription{get;set;}
公共字符串提供程序{get;set;}
public System.DateTime开始日期{get;set;}
public System.DateTime EndDate{get;set;}
公共字符串类型{get;set;}
}

如果您仍然遇到奇怪的行为,请发布内部异常

,因此,我们已经在类构造中发现了一些错误(请参见注释)。在本例中,您有两个变体

首先,逐步反序列化对象。创建服务xml(手动或从示例中复制粘贴)并对其进行反序列化,创建保修xml并对其进行反序列化,创建无保修资产列表xml并对其进行反序列化,最后创建带保修的资产xml并对其进行反序列化。在简单的课堂上更容易发现这样的错误并增加难度

第二种变体:实用程序。您需要像
xsd.exe yourXml.xml/c
xsd.exe yourXml.xsd/c
(请参阅文档中的详细信息)。
最后,您将获得包含所有必要类的xml.cs文件。只需修复数组属性,它们有时会被不正确地处理。

因此,我们已经在类构造中发现了一些错误(请参见注释)。在本例中,您有两个变体

首先,逐步反序列化对象。创建服务xml(手动或从示例中复制粘贴)并对其进行反序列化,创建保修xml并对其进行反序列化,创建无保修资产列表xml并对其进行反序列化,最后创建带保修的资产xml并对其进行反序列化。在简单的课堂上更容易发现这样的错误并增加难度

第二种变体:实用程序。您需要像
xsd.exe yourXml.xml/c
xsd.exe yourXml.xsd/c
(请参阅文档中的详细信息)。
最后,您将获得包含所有必要类的xml.cs文件。只需修复数组属性,它们有时会被不正确地处理。

内部异常是什么?另外,发布的XML缺少结束标记(
),我猜这不是实际的XML文档,因为它在根元素之前有“-”。(如果是,那就是问题所在……不要从XML文件的浏览器视图复制/粘贴。)我没有看到结束标记。服务类中没有类型字段。尝试逐个反序列化对象。您还缺少必需的xml定义标记:
我不确定这是否足以破坏xml本身,但添加它肯定不会有什么坏处?内部异常是什么?另外,发布的XML缺少结束标记(
),我猜这不是实际的XML文档,因为它在根元素之前有“-”。(如果是,那就是问题所在……不要从XML文件的浏览器视图复制/粘贴。)我没有看到结束标记。服务类中没有类型字段。尝试逐个反序列化对象。您还缺少必需的xml定义标记:
我不确定这是否足以破坏xml本身,但添加它肯定不会有什么坏处?谢谢您的回复。谢谢你对课程的批改。我仍然收到相同的错误。@user3930238您的保修类别应包含“Servi”
XmlSerializer serializer = new XmlSerializer(typeof(Machine));
using (System.IO.FileStream stream = new System.IO.FileStream(@"C:\Users\Administrator\Desktop\test.xml", System.IO.FileMode.Open))
{
     Machine machine = (Machine)serializer.Deserialize(stream);
     // do stuff with machine
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Machine
{        /// <remarks/>
    public MachineAsset Asset { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAsset
{
    public string Product { get; set; }

    public uint OrderNumber { get; set; }

    public uint ServiceTag { get; set; }

    public System.DateTime ShipDate { get; set; }

    [System.Xml.Serialization.XmlArrayItemAttribute("Warranty", IsNullable = false)]
    public MachineAssetWarranty[] Warranties { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAssetWarranty
{
    public MachineAssetWarrantyService Service { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Services { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAssetWarrantyService
{
    public string ServiceDescription { get; set; }

    public string Provider { get; set; }

    public System.DateTime StartDate { get; set; }

    public System.DateTime EndDate { get; set; }

    public string Type { get; set; }
}