Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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_Visual Studio_Xmlserializer - Fatal编程技术网

C# 如何将XML属性反序列化为对象,然后遍历对象

C# 如何将XML属性反序列化为对象,然后遍历对象,c#,xml,visual-studio,xmlserializer,C#,Xml,Visual Studio,Xmlserializer,我希望能够对XML文件中的车辆进行“操作”。理想情况下,我希望遍历所有车辆并计算它们的价格,然后更改它们是否在销售。然后,这些值将显示在UI中。我的代码反序列化XML文件,但我无法访问车辆的任何属性。我不需要将对象序列化回XML 我已尝试Console.WriteLine价格,但是当我运行代码时,它返回为0。我是否应该创建一系列ResponseGraphyVendorRegionVehicle?然后以某种方式将该类型的对象添加到数组中 这是XML文件: <?xml version="1.0

我希望能够对XML文件中的车辆进行“操作”。理想情况下,我希望遍历所有车辆并计算它们的价格,然后更改它们是否在销售。然后,这些值将显示在UI中。我的代码反序列化XML文件,但我无法访问车辆的任何属性。我不需要将对象序列化回XML

我已尝试Console.WriteLine价格,但是当我运行代码时,它返回为0。我是否应该创建一系列ResponseGraphyVendorRegionVehicle?然后以某种方式将该类型的对象添加到数组中

这是XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<Response>
  <Geography>
    <Vendor id="JOHN">
      <Region id="1"></Region>
      <Region id="2">
        <Vehicle Make="HONDA" Fuel="Gas" Price="12000" OnSale="Y" Account="JOHNH" />
        <Vehicle Make="ACURA" Fuel="Gas" Price="14100" OnSale="Y" Account="JOHNH" />
        <Vehicle Make="TOYOTA" Fuel="Gas" Price="8000" OnSale="N" Account="JOHNH" />
        <Vehicle Make="HYUNDAI" Fuel="Gas" Price="13000" OnSale="Y" Account="JOHNH" />
        <Vehicle Make="INFINITY" Fuel="Gas" Price="16000" OnSale="N" Account="JOHNH" />
      </Region>
      <Region id="3"></Region>
      <Region id="4"></Region>
    </Vendor>
  </Geography>
</Response>
这是创建的Paste Special Response.CS文件:

namespace XMLDeserializeExample
{
}

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Response
{

    private ResponseGeography geographyField;

    /// <remarks/>
    public ResponseGeography Geography
    {
        get
        {
            return this.geographyField;
        }
        set
        {
            this.geographyField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseGeography
{

    private ResponseGeographyVendor vendorField;

    /// <remarks/>
    public ResponseGeographyVendor Vendor
    {
        get
        {
            return this.vendorField;
        }
        set
        {
            this.vendorField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseGeographyVendor
{

    private ResponseGeographyVendorRegion[] regionField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Region")]
    public ResponseGeographyVendorRegion[] Region
    {
        get
        {
            return this.regionField;
        }
        set
        {
            this.regionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseGeographyVendorRegion
{

    private ResponseGeographyVendorRegionVehicle[] vehicleField;

    private byte idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Vehicle")]
    public ResponseGeographyVendorRegionVehicle[] Vehicle
    {
        get
        {
            return this.vehicleField;
        }
        set
        {
            this.vehicleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte id
    {
        get
        {
            return this.idField;
        }
        set
        {
            this.idField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ResponseGeographyVendorRegionVehicle
{

    private string makeField;

    private string fuelField;

    private ushort priceField;

    private string onSaleField;

    private string accountField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Make
    {
        get
        {
            return this.makeField;
        }
        set
        {
            this.makeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Fuel
    {
        get
        {
            return this.fuelField;
        }
        set
        {
            this.fuelField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public ushort Price
    {
        get
        {
            return this.priceField;
        }
        set
        {
            this.priceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string OnSale
    {
        get
        {
            return this.onSaleField;
        }
        set
        {
            this.onSaleField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Account
    {
        get
        {
            return this.accountField;
        }
        set
        {
            this.accountField = value;
        }
    }
}
命名空间XMLDeserializeExample
{
}
//注意:生成的代码可能至少需要.NET Framework 4.5或.NET Core/Standard 2.0。
/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公众部分班级反应
{
私人责任地理领域;
/// 
公共责任地理学
{
得到
{
返回此.geographyField;
}
设置
{
this.geographyField=值;
}
}
}
/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类响应记录
{
私人责任记录Yvender vendorField;
/// 
公共责任记录供应商
{
得到
{
返回这个.vendorField;
}
设置
{
this.vendorField=值;
}
}
}
/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类应答器
{
私人负责人区域[]区域字段;
专用字符串字段;
/// 
[System.Xml.Serialization.XmlElementAttribute(“区域”)]
公共响应图地区[]地区
{
得到
{
返回此.regionField;
}
设置
{
this.regionField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串id
{
得到
{
返回此.idField;
}
设置
{
this.idField=值;
}
}
}
/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类响应地理区域
{
私人负责人登记区域车辆[]车辆场;
专用字节字段;
/// 
[System.Xml.Serialization.xmlementAttribute(“车辆”)]
公共响应图YVENDORRegion车辆[]车辆
{
得到
{
把这辆车还给我;
}
设置
{
此.vehicleField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字节id
{
得到
{
返回此.idField;
}
设置
{
this.idField=值;
}
}
}
/// 
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类响应记录区域车辆
{
私有字符串makeField;
私有字符串字段;
私人乌肖特价格场;
Salefield上的私有字符串;
私有字符串accountField;
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串生成
{
得到
{
返回这个.makeField;
}
设置
{
this.makeField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共管柱燃料
{
得到
{
返回这个.fuelField;
}
设置
{
this.fuelField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共住房价格
{
得到
{
返回此.priceField;
}
设置
{
this.priceField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
出售公共字符串
{
得到
{
返回此.onSaleField;
}
设置
{
this.onSaleField=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字符串帐户
{
得到
{
返回此.accountField;
}
设置
{
this.accountField=值;
}
}
}
请让我知道如何更好地解释我自己。如果这些都没有意义,我道歉-欢迎来到我的周末lol


谢谢。

您需要使用内置在.Net中的XML序列化程序

首先创建一个类来表示XML文档数据:

public class Response
{
      public Geography Geography {get; set;}
}
public class Geography
{
       public Vendor Vendor{get;set;}
}
public class Vendor
{
      public List<Region> Regions {get;set;}
}
public class Region
{
}

然后,您可以迭代地理对象上的所有属性和内容。

您需要使用内置在.Net中的XML序列化程序

首先创建一个类来表示XML文档数据:

public class Response
{
      public Geography Geography {get; set;}
}
public class Geography
{
       public Vendor Vendor{get;set;}
}
public class Vendor
{
      public List<Region> Regions {get;set;}
}
public class Region
{
}

然后,您可以迭代地理对象上的所有属性和内容。

因为您的xml根显然是一个
响应
而不是
响应图形
,所以您必须反序列化为该类型:

string path = @"c:\XMLFile1.xml";
XmlSerializer ser = new XmlSerializer(typeof(response);
ResponseGeographyVendor i;
using (Stream reader = new FileStream(path,FileMode.Open)) 
{
    i = ((Response)ser.Deserialize(reader)).Geography.Vendor;
    Console.WriteLine(i.Price);
    Console.ReadLine();
}
序列化程序只能处理整个xml文档。你不能只写或读其中的一部分。所以只需使用xml,将其序列化为
string path = @"c:\XMLFile1.xml";
XmlSerializer ser = new XmlSerializer(typeof(response);
ResponseGeographyVendor i;
using (Stream reader = new FileStream(path,FileMode.Open)) 
{
    i = ((Response)ser.Deserialize(reader)).Geography.Vendor;
    Console.WriteLine(i.Price);
    Console.ReadLine();
}
var vehicle = i.Region[1].Vehicle[2];