通常我写一个类,并添加XML序列化,以它为我的Web服务。
[XmlRootAttribute(的ElementName =dsXmlSummary,ISNULLABLE =真)
公共类的Class1
{
//声明属性
//使调用数据库中提取数据和负荷特性
}
我工作的原因,我需要用严格的XSD一个项目,我已经按照有关使用XSD.EXE工具来创建基于XSD类的说明。我的跨pretation是,这个自动生成的类将取代我的正常序列化类。
如果是这样的话,我对将数据装入类的属性完全丧失。 我已经通过收集这从另外一条路走过:
[WebMethod的]
公共dsXmlSummary的getXML()
{
TextReader的读者=新的StreamReader(data.xml中);
dsXmlSummary DS =(dsXmlSummary)serializer.Deserialize(读卡器);
reader.Close();
}
不过,我也坐落在一个SQL数据库中的数据...我想我应该可以写一个方法来填补 dsXmlSummary
类,但是我无法找到在这样做的任何文件。所有的例子都像上述情况,从实际的物理XML文件加载或读取。
我试图测试出一个手动负载:
[WebMethod的]
公共dsXmlSummary的getXML()
{
dsXmlSummary XML =新dsXmlSummary();
xml.Items [0] .nameFirst =标记; //错误这里抛出:System.NullReferenceException:对象不设置到对象的实例。
xml.Items [0] .nameLast =吐温;
xml.Items [0]。键入=作家;
返回XML;
}
很显然,我要对此都错了。任何指导,大大AP preciated。
修改
我的WebMethod
[WebMethod的]
公共dsXmlSummary的getXML()
{
dsXmlSummary XML =新dsXmlSummary();
dsXmlSummaryAdmin_reports_xmlReports []项目=新dsXmlSummaryAdmin_reports_xmlReports [1];
项目[0] .nameFirst =标记; //错误还在这里抛出:System.NullReferenceException:对象不设置到对象的实例。
项目[0] .nameLast =吐温;
项目[0]。键入=作家;
xml.Items =项目;
返回XML;
}
自动生成的类
公共部分类dsXmlSummary {
私人dsXmlSummaryAdmin_reports_xmlReports [] itemsField;
///<备注/>
[System.Xml.Serialization.XmlElementAttribute(admin_reports_xmlReports)]
公共dsXmlSummaryAdmin_reports_xmlReports []项目{
得到 {
返回this.itemsField;
}
组 {
this.itemsField =价值;
}
}
}
解决方案
如果你作为一个字符串,你可以使用的 StringReader
。或者,如果你得到一个字节[]
您可以尝试的 的MemoryStream
。
[WebMethod的]
公共dsXmlSummary的getXML()
{
TextReader的读者=新StringReader(< dsXmlSummary><名字>马克< /姓>< / dsXmlSummary>);
dsXmlSummary DS =(dsXmlSummary)serializer.Deserialize(读卡器);
reader.Close();
}
关于你的手册的例子,你需要初始化你的项目的数组。
<编辑>新增 xml.Items [0] =新YourItemsType();
< /编辑>
[WebMethod的]
公共dsXmlSummary的getXML()
{
dsXmlSummary XML =新dsXmlSummary();
xml.Items =新YourItemsType [1]; //< - 初始化这里
xml.Items [0] =新YourItemsType(); //< - 初始化第一个对象
xml.Items [0] .nameFirst =标记; //错误这里抛出:System.NullReferenceException:对象不设置到对象的实例。
xml.Items [0] .nameLast =吐温;
xml.Items [0]。键入=作家;
返回XML;
}
Normally I write a class and add XML Serialization to it for my web services.
[XmlRootAttribute(ElementName = "dsXmlSummary", IsNullable=true)]
public class Class1
{
//declare properties
//make database calls to pull data and load properties
}
I am working on a project that requires me to use a strict XSD, I've followed instructions on using the XSD.EXE tool to create a class based on the XSD. My interpretation was that this auto-generated class would replace my normal serialized class.
If this is the case I am completely lost on loading the data into the class properties. I've gathered this from another walk through:
[WebMethod]
public dsXmlSummary getXML()
{
TextReader reader = new StreamReader("data.xml");
dsXmlSummary ds = (dsXmlSummary)serializer.Deserialize(reader);
reader.Close();
}
However the data I have is located in a SQL database...I figured I should be able to write a method to fill the dsXmlSummary
class, however I cannot find any documentation on doing this. All examples are like the above, loading or reading from an actual physical xml document.
I tried testing out a manual load:
[WebMethod]
public dsXmlSummary getXML()
{
dsXmlSummary xml = new dsXmlSummary();
xml.Items[0].nameFirst = "Mark"; //error thrown here: System.NullReferenceException: Object reference not set to an instance of an object.
xml.Items[0].nameLast = "Twain";
xml.Items[0].Type = "Writer";
return xml;
}
Obviously I'm going about this all wrong. Any guidance is greatly appreciated.
EDIT
My WebMethod
[WebMethod]
public dsXmlSummary getXML()
{
dsXmlSummary xml = new dsXmlSummary();
dsXmlSummaryAdmin_reports_xmlReports[] items = new dsXmlSummaryAdmin_reports_xmlReports[1];
items[0].nameFirst = "Mark"; //error still thrown here: System.NullReferenceException: Object reference not set to an instance of an object.
items[0].nameLast = "Twain";
items[0].Type = "Writer";
xml.Items = items;
return xml;
}
The Auto generated class
public partial class dsXmlSummary {
private dsXmlSummaryAdmin_reports_xmlReports[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("admin_reports_xmlReports")]
public dsXmlSummaryAdmin_reports_xmlReports[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
解决方案
If you get your XML from the database as a string you can use a StringReader
. Or if you get a byte[]
you may try a MemoryStream
.
[WebMethod]
public dsXmlSummary getXML()
{
TextReader reader = new StringReader("<dsXmlSummary><FirstName>Mark</FirstName></dsXmlSummary>");
dsXmlSummary ds = (dsXmlSummary)serializer.Deserialize(reader);
reader.Close();
}
Regarding your "manual" example you need to initialize your Items array.
<edit> Added xml.Items[0] = new YourItemsType();
</edit>
[WebMethod]
public dsXmlSummary getXML()
{
dsXmlSummary xml = new dsXmlSummary();
xml.Items = new YourItemsType[1]; // <-- initialize here
xml.Items[0] = new YourItemsType(); // <-- initialize first object
xml.Items[0].nameFirst = "Mark"; //error thrown here: System.NullReferenceException: Object reference not set to an instance of an object.
xml.Items[0].nameLast = "Twain";
xml.Items[0].Type = "Writer";
return xml;
}
相关推荐
最新文章