You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从C#导出类至指定XML Persistence Format格式文件?

Got it, let's break down how to generate that exact Microsoft-specific rowset XML schema from your C# class—no MSSQL or DataSet default exports required. The key issue here is that DataSet's auto-generated schema doesn't include those proprietary namespaces and metadata attributes (like rs:number, dt:type, etc.), so we need to build the schema manually or hook into custom serialization logic. Here's your actionable plan:


Solution Approach 1: Manually Build the XmlSchema Instance

This gives you full control to replicate every detail of the target schema. Use the System.Xml.Schema namespace to construct each element and attribute precisely.

Step-by-Step Code Example

using System.Xml;
using System.Xml.Schema;

public XmlSchema GenerateTargetSchema()
{
    // Initialize schema with all required namespaces
    var schema = new XmlSchema();
    schema.Namespaces.Add("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
    schema.Namespaces.Add("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
    schema.Namespaces.Add("rs", "urn:schemas-microsoft-com:rowset");
    schema.Namespaces.Add("z", "#RowsetSchema");
    schema.Id = "RowsetSchema";

    // Define the <s:ElementType name='row'>
    var rowElementType = new XmlSchemaElementType();
    rowElementType.Name = "row";
    rowElementType.ContentModel = new XmlSchemaSimpleContent();
    rowElementType.Content = new XmlSchemaSimpleContentExtension
    {
        BaseTypeName = new XmlQualifiedName("rowbase", "urn:schemas-microsoft-com:rowset")
    };

    // Helper method to create AttributeTypes with all metadata
    XmlSchemaAttributeType CreateAttributeType(string name, string rsNumber, string dtType, bool isNullable = false)
    {
        var attrType = new XmlSchemaAttributeType();
        attrType.Name = name;

        // Add rs:number attribute
        attrType.UnhandledAttributes.Add(
            new XmlAttribute("number", "urn:schemas-microsoft-com:rowset") { Value = rsNumber });

        // Add maybenull/writeunknown attributes as needed
        if (!isNullable)
        {
            attrType.UnhandledAttributes.Add(
                new XmlAttribute("maybenull", "urn:schemas-microsoft-com:rowset") { Value = "false" });
        }
        attrType.UnhandledAttributes.Add(
            new XmlAttribute("writeunknown", "urn:schemas-microsoft-com:rowset") { Value = "true" });

        // Define the datatype with dt:* attributes
        var dataType = new XmlSchemaSimpleType();
        var restriction = new XmlSchemaSimpleTypeRestriction();
        restriction.BaseTypeName = new XmlQualifiedName(dtType, "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");

        // Add datatype-specific attributes (adjust based on your target schema)
        if (dtType == "i2")
        {
            restriction.UnhandledAttributes.Add(
                new XmlAttribute("maxLength", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882") { Value = "2" });
            restriction.UnhandledAttributes.Add(
                new XmlAttribute("precision", "urn:schemas-microsoft-com:rowset") { Value = "5" });
            restriction.UnhandledAttributes.Add(
                new XmlAttribute("fixedlength", "urn:schemas-microsoft-com:rowset") { Value = "true" });
        }
        else if (dtType == "dateTime")
        {
            restriction.UnhandledAttributes.Add(
                new XmlAttribute("dbtype", "urn:schemas-microsoft-com:rowset") { Value = "timestamp" });
            restriction.UnhandledAttributes.Add(
                new XmlAttribute("maxLength", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882") { Value = "16" });
            // Add scale/precision/fixedlength as needed
        }

        dataType.Content = restriction;
        attrType.Content = dataType;
        return attrType;
    }

    // Add all your AttributeTypes to the ElementType
    rowElementType.Attributes.Add(CreateAttributeType("Idlog", "1", "i2", false));
    rowElementType.Attributes.Add(CreateAttributeType("ExpDate", "2", "dateTime", false));
    rowElementType.Attributes.Add(CreateAttributeType("ExpOper", "3", "string", false));
    rowElementType.Attributes.Add(CreateAttributeType("ImpDate", "4", "dateTime", true));
    // Repeat for ImpOper, MaxDate, NameWS...

    // Add the ElementType to the schema
    schema.Items.Add(rowElementType);

    // Compile to validate the schema (optional but recommended)
    var schemaSet = new XmlSchemaSet();
    schemaSet.Add(schema);
    schemaSet.Compile();

    return schema;
}

Solution Approach 2: Bind to C# Class with Custom XmlSchemaProvider

If you want to tie the schema generation directly to your C# model, use the XmlSchemaProviderAttribute to replace the default schema logic with your manual build code.

Example for Your Model Class

[XmlSchemaProvider("GetSchema")]
public class YourRowModel : IXmlSerializable
{
    public int Idlog { get; set; }
    public DateTime ExpDate { get; set; }
    // Add other properties...

    public static XmlSchema GetSchema(XmlSchemaSet schemaSet)
    {
        var schema = GenerateTargetSchema(); // Reuse the method from Approach 1
        schemaSet.Add(schema);
        return schema;
    }

    // Implement IXmlSerializable's ReadXml/WriteXml to handle custom serialization
    public void ReadXml(XmlReader reader)
    {
        // Parse XML attributes into your properties
        Idlog = int.Parse(reader.GetAttribute("Idlog"));
        ExpDate = DateTime.Parse(reader.GetAttribute("ExpDate"));
        // ...
    }

    public void WriteXml(XmlWriter writer)
    {
        // Write properties as XML attributes matching the target schema
        writer.WriteAttributeString("Idlog", Idlog.ToString());
        writer.WriteAttributeString("ExpDate", ExpDate.ToString("yyyy-MM-ddTHH:mm:ss"));
        // ...
    }
}

Why DataSet Doesn't Work

DataSet's schema generation is designed for generic relational data, not the proprietary Microsoft Rowset schema format. It won't include the uuid: namespaces or rs:/dt: metadata attributes you need—so manual construction is the only way to get an exact match.

内容的提问来源于stack exchange,提问作者Noisy88

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 08:32:43