使用XSD类生成XML

时间:2015-04-24 07:47:34

标签: c# linq class serialization xsd

我想通过使用xsd生成的c#类生成一个xml 我创建了一个linq查询,将查询结果保存在var ReportData中,我应该使用我的c#生成的类来保存这些数据。但是当我执行var data = new FlashListFlash {ECUtype = modelType};查询后数据为空。任何人都可以帮我解决这个问题吗?

这就是我所做的

string modelType = null;

LinqSqlDataContext db = new LinqSqlDataContext();
var reportData = from Vin in db.Vin_Ecus
                 from Global in db.Info_Globals
                              .Where(w =>
                                     w.NHard == Vin.NHard &&
                                     w.NVerHard == Vin.NVerHard &&
                                     w.NVerSoft == Vin.NVerSoft)
                              join Associate in db.InfoProg_wiTECH_Associas
                              on Global.NomeFile equals Associate.KeyJoined
                              where Vin.Vin == vinValue 
                  select new { modelType = Associate.Model_Type };


                  var data = new FlashListFlash { ECUtype = modelType };
                  //THIS IS MY Vin value probelm
                  //var data = new FlashList { Vin = vinValue}?????????????????????
                  var serializer = new XmlSerializer(typeof(FlashListFlash));
                  using (var stream = new StreamWriter("D:\\test.xml"))
                  serializer.Serialize(stream, data);

这是我从.xsd文件生成的c#类

using System.Xml.Serialization;


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class FlashList {

    private FlashListFlash flashField;

    private string vinField;

    /// <remarks/>
    public FlashListFlash flash {
        get {
            return this.flashField;
        }
        set {
            this.flashField = value;
        }
    }

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

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class FlashListFlash {

    private string eCUtypeField;

    private string valueField;

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

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

这是我的XSD文件

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="FlashList">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="flash">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="ECUtype"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute type="xs:string" name="Vin"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:0)

模型类型赋值使用的是null的局部变量:

string modelType = null;
....
var data = new FlashListFlash { ECUtype = modelType };

您应该分配链接查询结果:

var data = new FlashListFlash { ECUtype = reportData.modelType };

vin值(vinValue)未定义...所以不知道代码如何编译,除非你没有在这里粘贴所有代码。