1

I have a very simple application. MainUI has the List of CustomClass. I pass this list to WCF service. WCF Service further needs to save these objects in database.

I am using Open XML in our sql stored procedure to get better performnace. But i don't know how to convert my List of Objects to XML.

If i have a datatable, it'll be easy as datatables have methods to get the XML out of them. But how to use for List of objects.

I completly understand that if my List is coming over the WCF, it is getting serialized properly. But what should i exactly need to do.


Can't repro this.

I run your transformation with Saxon 9.1.07 and the result is;

<div class="box">
      Box[2,5] = (1)
      <title>This is box TWO</title></div><div class="box">
      Box[3,6] = (12)
      <title>This is box THREE</title></div><div class="box">
      Box[1,7] = (103)
      <title>This is box ONE</title></div>

As we see, the positions are correct (still not 1,2,3 -- because white-space-only nodes are also present).

The same result is produced by XQSharp (XmlPrime).

AltovaXML2009 (XML-SPY) produces even this:

<?xml version="1.0" encoding="UTF-8"?><div class="box">
    Box[2,1] = (1)
      <title>This is box TWO</title></div><div class="box">
    Box[3,2] = (12)
      <title>This is box THREE</title></div><div class="box">
    Box[1,3] = (103)
      <title>This is box ONE</title></div>

which means that it uses an XML parser that strips-off white-space-only text nodes.

An improved version of this transformation would exclude white-space-only nodes:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="body">
        <xsl:apply-templates>
          <xsl:sort select="@priority" data-type="number"/>
        </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="box">
    <div class="box">
      Box[<xsl:value-of select="count(preceding-sibling::box) + 1" />
      <xsl:text>,</xsl:text>
      <xsl:value-of select="position()"/>
      <xsl:text>] = (</xsl:text>
      <xsl:value-of select="@priority"/>)
      <xsl:copy-of select="title"/></div>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied against the provided XML document:

<body>
    <box priority="103">
        <title>This is box ONE</title>
    </box>
    <box priority="1">
        <title>This is box TWO</title>
    </box>
    <box priority="12">
        <title>This is box THREE</title>
    </box>
</body>

the wanted, correct result is produced:

<div class="box">
      Box[2,1] = (1)
      <title>This is box TWO</title>
</div>
<div class="box">
      Box[3,2] = (12)
      <title>This is box THREE</title>
</div>
<div class="box">
      Box[1,3] = (103)
      <title>This is box ONE</title>
</div>

Notes: xsl:strip-space is used to exclude the white-space-only nodes even from being parsed.

Conclusion: The reported result is due either to using a buggy XSLT processor, or to performing a transformation that is different than the provided one.

4

3 に答える 3

1

IMO、属性XmlSerializerクラスを使用したXMLシリアル化の制御を調べ、場合によってはCustomClassと並列のコンテナークラスを作成します。List<>デフォルトのXMLシリアライザーでは自動的にシリアル化できませんが、配列は自動的にシリアル化できます。

于 2012-05-27T14:03:46.847 に答える
1

XmlSerializerまたはDataContractSerializerを使用する 2 つの方法があります。

于 2012-05-27T13:50:50.383 に答える
1

リストを xml に変換するコード: リスト名 GridDetails

 void ConvertToXml()
        {

            string xmlString = ConvertObjectToXMLString(GridDetails);
            // Save C# class object into Xml file
            XElement xElement = XElement.Parse(xmlString);
            xElement.Save(@"C:\Users\user\Downloads\userDetail.xml");
        }


 static string ConvertObjectToXMLString(object classObject)
    {
        string xmlString = null;
        XmlSerializer xmlSerializer = new XmlSerializer(classObject.GetType());
        using (MemoryStream memoryStream = new MemoryStream())
        {
            xmlSerializer.Serialize(memoryStream, classObject);
            memoryStream.Position = 0;
            xmlString = new StreamReader(memoryStream).ReadToEnd();
        }
        return xmlString;
    }
于 2016-04-01T11:27:33.857 に答える