Im working with a legacy application that needs to convert an object to XML in this format:
<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>
But i have no idea how to convert my object to this type of XML, in our previous application that made this conversion, we used ASP and simply only did this:
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
Is there any way to convert an C# object to this format of XML? The object is an list like this:
List<MyClass> obj = await GetRows();
And i want to convert obj to XML.
Ive read this: How can I import/export from XML Persistence Format files?
with no luck, i dont really understand what they mean.
CodePudding user response:
He is code using 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
{
}
}