c# - System.Type 到 XmlSchema 内置类型

标签 c# xml xsd

我正在将一些类属性写入 XmlSchema现在我想在 <xs:element type="xs:string"> 中写类型.

是否有一些映射类所以我不必自己编写 switch-case

public class Foo
{
    public string Bar { get; set; }
}

public void WriteProperty()
{
    // get the property that is a string
    PropertyInfo barProperty;
    XmlSchemaElement barElement;

    // I don't want this huge switch case for all basic properties.
    switch(barProperty.PropertyType.FullName)
    {
        case "System.String":
            barElement.SchemaTypeName = new QualifiedName("xs:string");
            break;
        // also for int, and bool, and long....


        default:
            //do other stuff with types that are not default types
            break;
    }
}

最佳答案

.NET 框架没有映射类或函数来执行您需要的操作。

我建议创建一个映射字典而不是使用一个大开关:

static Dictionary<string, string> TypeMap = new Dictionary<string, string>() {
  { "System.String", "xs:string" },
  { "System.Int32", "xs:int" },
  . . . 
};

. . . 

  string schemaTypeName;
  if (TypeMap.TryGetValue(barProperty.PropertyType.FullName, out schemaTypeName)) {
    barElement.SchemaTypeName = new QualifiedName("xs:string");
  } else {
    //do other stuff with types that are not default types
  }

关于c# - System.Type 到 XmlSchema 内置类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18852639/

相关文章:

c# - 我是否正确加载了这个 xml 文件?

xsd - xml 模式 maxOccurs = xs :all 内无界

.net - XmlReader 从架构中获取元素默认值

xml - XSD 无、其中之一或两者

c# - 使用 C# 使用 Linq 更新 XML

javascript - 如何使用 JavaScript 循环遍历一个长字符串以在每次匹配后插入一个新字符串

c# - 使用 XmlWriterSettings 和 XmlSerializer 编写 XML 片段会产生额外的字符

xml - XML 中的组元素

C# 和 VB.NET 引发事件差异

c# - 将 Char 变量转换为整数