在DataSet中设置正确的属性类型 [英] Setting correct attribute types in DataSet

查看:150
本文介绍了在DataSet中设置正确的属性类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在尝试用C#创建XML文档和相应的XSD架构。

我可以创建XML并获得XSD,但XSD并不是100%正确。

我的元素的属性类型总是xs:string in XSD,这是不正确的。

其中一些需要是整数而其他字符串。

我是新手,所以答案可能很明显但是我只是不确定我是怎么做的。我想我需要使用XmlSerializer,但我不知道从哪里开始。



下面是一个简单的例子我正在尝试:



Hi,
I''ve been trying to create an XML document and corresponding XSD schema in C#.
I can create the XML fine and get the XSD, but the XSD isn''t 100% correct.
The attribute type for my elements is always xs:string in the XSD, which is not correct.
Some of them need to be integer and others string.
I''m new to this so the answer might be pretty obvious but I''m just not sure how I go about doing it. I think I need to use XmlSerializer for it from what I have read but I''m not sure where to start with it.

Below of a a simple example of what I am trying to do:

class Person
{
    public int ID { get; set; }
    public String Name { get; set; }
    public int Age { get; set; }

    public XElement GetXML()
    {
        var person = new XElement("Person", new XAttribute("ID", ID));
        person.Add(new XElement("Name", Name));
        person.Add(new XElement("Age", Age));
        return person;
    }
}

static void Main(string[] args)
{
    var set = new DataSet("Test");
    var root = new XElement("root");

    var p1 = new Person() {ID = 1, Name = "P1", Age = 32};
    var p2 = new Person() {ID = 2, Name = "P2", Age = 40};

    root.Add(p1.GetXML());
    root.Add(p2.GetXML());

    set.ReadXml(root.CreateReader());
    var xml = set.GetXml();
    var schema = set.GetXmlSchema();

    Console.WriteLine(schema);
    Console.WriteLine(xml);
    Console.ReadKey(true);
}





此代码生成以下XSD和XML



This code produces the following XSD and XML

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="root" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:

msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="root" msdata:IsDataSet="true" msdata:Locale="en-US">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Person">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Name" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
              <xs:element name="Age" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
            </xs:sequence>
            <xs:attribute name="ID" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

<root>
  <Person ID="1">
    <Name>P1</Name>
    <Age>32</Age>
  </Person>
  <Person ID="2">
    <Name>P2</Name>
    <Age>40</Age>
  </Person>
</root>





如您所见,在XSD中,Age元素被赋予一种字符串。我假设这是默认情况,但我看不出我应该如何设置它为不同的类型,如整数。



任何帮助将不胜感激。

谢谢



As you can see, in the XSD the Age element is given a type of string. I assume this is by default, but I cannot see how I am supposed to set this to a different type, such as integer.

Any help would be appreciated.
Thanks

推荐答案

它可能会也可能不会为您提供确切类型的数据集列,但至少省略字符串类型。

对代码进行这一小改动并重新运行。

It may or may not give you the exact type of dataset Columns, But at least omit the the string type.
Make this small change in your code and rerun.
set.ReadXml(root.CreateReader(),XmlReadMode.InferTypedSchema);





欲了解更多信息,请阅读 XMLReadMode Enum


这篇关于在DataSet中设置正确的属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆