C# 使用XSD类生成XML
我想使用从xsd生成的c类生成xml 我做了一个linq查询,将查询结果保存在var ReportData中,我应该使用我的c生成的类来保存此数据;查询后,数据为空。有人能帮我修一下吗 这就是我所做的C# 使用XSD类生成XML,c#,linq,class,serialization,xsd,C#,Linq,Class,Serialization,Xsd,我想使用从xsd生成的c类生成xml 我做了一个linq查询,将查询结果保存在var ReportData中,我应该使用我的c生成的类来保存此数据;查询后,数据为空。有人能帮我修一下吗 这就是我所做的 string modelType = null; LinqSqlDataContext db = new LinqSqlDataContext(); var reportData = from Vin in db.Vin_Ecus from Global in
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>
modeltype分配使用的局部变量为null:
string modelType = null;
....
var data = new FlashListFlash { ECUtype = modelType };
您应该分配链接查询结果:
var data = new FlashListFlash { ECUtype = reportData.modelType };
vin值未定义…因此不知道代码如何编译,除非您没有将所有代码粘贴到此处。已定义但未粘贴到此处。我的代码已编译,但xml文件为空。我尝试了var data=newflashlistflash{ECUtype=reportData.modelType};但modeltype不是reportData的属性,所以它是red://只是为了进一步澄清我的问题:在生成的c类中,我必须对一个主FlashList和一个子类FlashListFlash进行分类,我应该给它们赋值。然后将结果序列化并保存在FlashList中的一个xml文件中,有一个Vin字段只有一个字符串,还有一个FlashListFlash,我应该给出一组已经存在于reportData中的值