将 C# 对象转换为 XML 持久化格式

Convert C# object to XML persistence format

我正在使用需要将对象转换为以下格式的 XML 的遗留应用程序:

<xml xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"  
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"  
xmlns:rs="urn:schemas-microsoft-com:rowset"  
xmlns:z="#RowsetSchema">  
  <s:Schema id="RowsetSchema">  
    <s:ElementType name="row" content="eltOnly" rs:updatable="true">  
      <s:AttributeType name="ShipperID" rs:number="1"  
        rs:basetable="shippers" rs:basecolumn="ShipperID" 
        rs:keycolumn="true">  
        <s:datatype dt:type="int" dt:maxLength="4" rs:precision="10"  
          rs:fixedlength="true" rs:maybenull="false"/>  
      </s:AttributeType>  
      <s:AttributeType name="CompanyName" rs:number="2"  
        rs:nullable="true" rs:write="true" rs:basetable="shippers"  
        rs:basecolumn="CompanyName">  
        <s:datatype dt:type="string" dt:maxLength="40" />  
      </s:AttributeType>  
      <s:AttributeType name="Phone" rs:number="3" rs:nullable="true"  
        rs:write="true" rs:basetable="shippers"  
        rs:basecolumn="Phone">  
        <s:datatype dt:type="string" dt:maxLength="24"/>  
      </s:AttributeType>  
      <s:extends type="rs:rowbase"/>  
    </s:ElementType>  
  </s:Schema>  
 
  <rs:data>  
    <z:row ShipperID="1" CompanyName="Speedy Express"  
      Phone="(503) 555-9831"/>  
    <z:row ShipperID="2" CompanyName="United Package"  
      Phone="(503) 555-3199"/>  
    <z:row ShipperID="3" CompanyName="Federal Shipping"  
      Phone="(503) 555-9931"/>  
  </rs:data>  
</xml> 

https://docs.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/xml-persistence-format

但我不知道如何将我的对象转换为这种类型的 XML,在我们之前进行这种转换的应用程序中,我们使用了 ASP 并且只是这样做了:

set Stream = Server.CreateObject("ADODB.Stream")
        Stream.Open

    Header.MoveNext
    set Rows = Header.NextRecordset
        Rows.Save Strom, AdPersistXml
        
    xmlkod = Stream.ReadText()
    
    with Response
        .ContentType    = "application/xm"
        .Expires        = -99
        .AddHeader      "content-disposition", "attachment;filename=" & filnamn & ".xml"
        .Write          "<?xml version='1.0' encoding='ISO-8859-1'?>" & VbCrLf
        .Write          xmlkod
    end with
    
    set SP = nothing
    set Stream = nothing

有什么方法可以将 C# 对象转换为 XML 这种格式吗? 该对象是这样的列表:

List<MyClass> obj = await GetRows();

我想将 obj 转换为 XML。

我读过这个: How can I import/export from XML Persistence Format files?

运气不好,我不太明白他们的意思。

他的代码使用 xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication29
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument doc = GetDoc();

            XNamespace rsNs = doc.Root.GetNamespaceOfPrefix("rs");
            XNamespace zNs = doc.Root.GetNamespaceOfPrefix("z");
            XElement data = new XElement(rsNs + "data");
            doc.Root.Add(data);
            List<MyClass> obj = new List<MyClass>();
            foreach(MyClass row in obj)
            {
                XElement xRow = new XElement(zNs + "row", new object[] {
                   new XAttribute("ShipperID", 1),
                   new XAttribute("CompanyName", "Speedy Express"),
                   new XAttribute("Phone", "(503) 555-9831")
                });

                data.Add(xRow);                    

            }

            string xmlStr = doc.ToString();
 

        }
        static XDocument GetDoc()
        {
            string xml = @"<xml xmlns:s=""uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882""  
                xmlns:dt=""uuid:C2F41010-65B3-11d1-A29F-00AA00C14882""  
                xmlns:rs=""urn:schemas-microsoft-com:rowset""  
                xmlns:z=""#RowsetSchema"">  
                  <s:Schema id=""RowsetSchema"">  
                    <s:ElementType name=""row"" content=""eltOnly"" rs:updatable=""true"">  
                      <s:AttributeType name=""ShipperID"" rs:number=""1""  
                        rs:basetable=""shippers"" rs:basecolumn=""ShipperID"" 
                        rs:keycolumn=""true"">  
                        <s:datatype dt:type=""int"" dt:maxLength=""4"" rs:precision=""10""  
                          rs:fixedlength=""true"" rs:maybenull=""false""/>  
                      </s:AttributeType>  
                      <s:AttributeType name=""CompanyName"" rs:number=""2""  
                        rs:nullable=""true"" rs:write=""true"" rs:basetable=""shippers""  
                        rs:basecolumn=""CompanyName"">  
                        <s:datatype dt:type=""string"" dt:maxLength=""40"" />  
                      </s:AttributeType>  
                      <s:AttributeType name=""Phone"" rs:number=""3"" rs:nullable=""true""  
                        rs:write=""true"" rs:basetable=""shippers""  
                        rs:basecolumn=""Phone"">  
                        <s:datatype dt:type=""string"" dt:maxLength=""24""/>  
                      </s:AttributeType>  
                      <s:extends type=""rs:rowbase""/>  
                    </s:ElementType>  
                  </s:Schema>
             </xml>";

            XDocument doc = XDocument.Parse(xml);
            return doc;
        }
    }
    public class MyClass
    {
    }
 
}