本文介绍了使用xsd.exe创建的类时出现编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个XML文件,我想反序列化它,所以我使用xsd.exe创建一个.XSD 1218.xml 之后我使用xsd.exe / classes 1218.xsd生成关联的类 然后,我将生成的.cs放在我的C#项目中,我在自动生成的文件中得到了一个编译错误: C:\ Visis Studio Projects\Test\AutoTest\source\1218.cs(133,68):错误CS0120:对象引用是非静态字段,方法或属性'ScriptSessionOutputScriptOutput.System.get'所需的内容 C:\ Visis Studio Projects\Test\AutoTest\source\1218.cs(136,9):(相关位置) ) C:\ Visis Studio Projects\Test\AutoTest\source\1218.cs(133,75):错误CS0117:'System.Array'不包含'Xml'的定义 参考的行是: [System.Xml.Serialization.XmlElementAttribute(" System",Form = System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable = true)] 并且是该班级的成员: //< remarks /> [System.CodeDom.Compiler.GeneratedCodeAttribute(" xsd"," 2.0.50727.42")] [System.SerializableAttribute()] [ System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(" code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 公共部分类ScriptSessionOutputScriptOutput { private ScriptSessionOutputScriptOutputSystem [] systemField; 私人ScriptSessionOutputScriptOutputInfo [] infoField; private ScriptSessionOutputScriptOutputScriptExit [] scriptExitField; 私人字符串scriptRunIdField; 私人字符串scriptField; private string versionField; 私人字符串timeField; ///< remarks /> [System.Xml.Serialization.XmlElementAttribute(" System",Form = System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable = true)] public ScriptSessionOutputScriptOutputSystem [] System { get { return this.systemField; } set { this.systemField = value; } } //继续........ 奇怪的是这是第一次在代码中出现类似的行时,它不会引发错误: ///< remarks /> [System.CodeDom.Co mpiler.GeneratedCodeAttribute(" xsd"," 2.0.50727.42")] [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 ScriptSessionOutput { private ScriptSessionOutputScriptOutput [] scriptOutputField; 私人字符串macAddressField; private string consoleField; private string dateField; private string timeField; ///< remarks /> [System.Xml.Serialization.XmlElementAttribute(" ScriptOutput",Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public ScriptSessionOutputScriptOutput [] ScriptOutput { get { return this.scriptOutputField; } set { this.scriptOutputField = value; } } //继续........ } 我尝试使用Visual Studio 2003的xsd.exe生成类,更改声明,注释有问题的行(我执行程序时我的对象是空的)以及我能想到的其他所有东西,但没有扫。它之前使用的是一个简单的例子,但是当xml变得有点复杂时,它就无法编译。以下是我的.xml示例: < ScriptSessionOutput MacAddress =" 001AE99D3C29"控制台= QUOT; 001AE99D3C29"日期= QUOT 11 /二千零八分之二十一"时间=< 2:20:17 PM"> < ScriptOutput w Script =" AutoTest .vshost"版本= QUOT; 8.0.50727.42"时间=< 2:20:17 PM"> < System Time =" 2: 20:17 PM">脚本控制台,用于会话ID 1218< / System> < Info Time =" 2:20:18 PM">模拟返回:True< / Info> < Info Time =" 2:20:23 PM">加载地图编号:1000< / Info> < ScriptExit ExitStatus ="失败"时间=< 2:44:14 PM">脚本提前结束< / ScriptExit> < / ScriptOutput> < / ScriptSessionOutput>I got an XML file and I want to deserialize it so I create an .XSD using xsd.exe 1218.xml After that I generate the associated class using xsd.exe /classes 1218.xsd Then, I put the resulting .cs inside my C# project and I got a compiling error inside the auto-generated file :C:\Visual Studio Projects\Test\AutoTest\source\1218.cs(133,68): error CS0120: An object reference is required for the nonstatic field, method, or property 'ScriptSessionOutputScriptOutput.System.get'C:\Visual Studio Projects\Test\AutoTest\source\1218.cs(136,9): (Related location)C:\Visual Studio Projects\Test\AutoTest\source\1218.cs(133,75): error CS0117: 'System.Array' does not contain a definition for 'Xml'The line reffered is : [System.Xml.Serialization.XmlElementAttribute("System", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)]And is member of the class:// <remarks/>[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")][System.SerializableAttribute()][System.Diagnostics.DebuggerStepThroughAttribute()][System.ComponentModel.DesignerCategoryAttribute("code")][System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]public partial class ScriptSessionOutputScriptOutput{ private ScriptSessionOutputScriptOutputSystem[] systemField; private ScriptSessionOutputScriptOutputInfo[] infoField; private ScriptSessionOutputScriptOutputScriptExit[] scriptExitField; private string scriptRunIdField; private string scriptField; private string versionField; private string timeField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("System", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)] public ScriptSessionOutputScriptOutputSystem[] System { get { return this.systemField; } set { this.systemField = value; } }//continuing ........}The strange this is that the first time that a similar line appear in the code, it does not raise en error :/// <remarks/>[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")][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 ScriptSessionOutput{ private ScriptSessionOutputScriptOutput[] scriptOutputField; private string macAddressField; private string consoleField; private string dateField; private string timeField; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute("ScriptOutput", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] public ScriptSessionOutputScriptOutput[] ScriptOutput { get { return this.scriptOutputField; } set { this.scriptOutputField = value; } }//continuing ........}I try to generate the class with the xsd.exe of Visual studio 2003, alter the declaration, comment the problematic line (my object is empty when I execute the program) and every thing else I can think of but nothing work. It was working with a simple example before but when the xml get a little bit more complicated it doesn't compile. Here is a sample of my .xml :<ScriptSessionOutput MacAddress="001AE99D3C29" Console="001AE99D3C29" Date="11/21/2008" Time="2:20:17 PM"> <ScriptOutput w Script="AutoTest.vshost" Version="8.0.50727.42" Time="2:20:17 PM"> <System Time="2:20:17 PM">Script controling console for session id 1218</System> <Info Time="2:20:18 PM">Simulation returned : True</Info> <Info Time="2:20:23 PM">Loading map number : 1000</Info><ScriptExit ExitStatus="Failure" Time="2:44:14 PM">Script ended prematurely</ScriptExit></ScriptOutput></ScriptSessionOutput> 推荐答案我想我刚从其他人继承的架构遇到了同样的问题。您的XML元素ScriptSessionOutputScriptOutput具有名为"System"的子元素。生成的代码声明名为"System"的属性。在课堂上,引用"System.whatever"不编译,因为编译器认为你正在尝试读取System属性,而不是引用System命名空间。 我希望有一种方法可以让XSD.EXE将属性重命名为其他属性比生成的代码中的System。但我还没有找到。我真的不想手工编辑生成的代码。I think I just ran into the same problem with a schema I inherited from someone else. Your XML element, ScriptSessionOutputScriptOutput, has a child element named "System". The generated code is declaring a property named "System". Within the class, references to "System.whatever" don't compile, because the compiler thinks you are trying to read the System property, rather than referencing the System namespace.I am hoping that there's a way to get XSD.EXE to rename the property to something other than System in the generated code. But I haven't found that yet. I really don't want to hand-edit the generated code. 这篇关于使用xsd.exe创建的类时出现编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-15 11:55