将XML转换为c#对象

时间:2013-02-21 19:20:04

标签: xml c#-4.0 object c#-2.0

我正在尝试创建一个从目录中获取小xml文件并将其作为对象的类,我总是遇到属性问题。我希望这个类有一个维度列表,以便可以调用它。 Dimension [x] .id,Dimension [x] .name ....等。该类将被多次引用,但我希望它只在第一次将xml填充到对象中。

这是XML,感谢高级:

    <?xml version="1.0"?>
     <dimensions>
      <dimensions id="0" name="Test"  serverAddress = "cm.dt.funcom.com" port = "7509" />
      <dimensions id="1" name="Atlantean"  serverAddress = "cm.d1.funcom.com" port = "7501" />
      <dimensions id="2" name="Rimor"  serverAddress = "cm.d2.funcom.com" port = "7502" />
     </dimensions>

1 个答案:

答案 0 :(得分:2)

我会尝试一些可以消除属性焦虑的东西。

生成(或使用现有的)适用于您的XML的XSD。像这样的东西:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="dimensions">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" name="dimensions">
          <xsd:complexType>
            <xsd:attribute name="id" type="xsd:unsignedByte" use="required" />
            <xsd:attribute name="name" type="xsd:string" use="required" />
            <xsd:attribute name="serverAddress" type="xsd:string" use="required" />
            <xsd:attribute name="port" type="xsd:unsignedShort" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

使用.NET提供的xsd.exe工具(Visual Studio命令提示符可以很好地为您设置路径)生成类(假设上面的XSD保存为converting-xml-into-a-c-sharp-object.xsd):

xsd /c <fullpath-if-not-in-the-current-folder>converting-xml-into-a-c-sharp-object.xsd

生成的代码类似于(只是标题):

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.17929
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <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 dimensions {

    private dimensionsDimensions[] dimensions1Field;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("dimensions")]
    public dimensionsDimensions[] dimensions1 {
        get {
            return this.dimensions1Field;
        }
        set {
            this.dimensions1Field = value;
        }
    }
}

下一步是编写引用此生成的类的代码;你可以找到很多很多参考文献;引言可以是this on SO

上面的XSD(“俄罗斯娃娃”)创作风格为可能看起来不聪明的类提供了名称; here是一种可能的解决方法。