找到你要的答案

Q:XML STRING to XSLT to C# class object

Q:XML字符串XSLT C #类对象

I don't have much experience in XSLT transformation so thought may be i can get some help here.

I am receiving this big xml string from through MSMQ in my .NET program. Lets says example as below:

<ROOT>
<PO>
  <PONUM> A101 </PONUM>
  <DESCRIPTION> THis is sample  PO </DESCRIPTION>
  <ADDRESS> Addresss here </ADDRESS>
  <ExtraInfo1> Any Info </ExtraInfo1>
  <ExtraInfo2> Any Info </ExtraInfo2>

<POLINE> 
  <POLINENUM> Line1 </POLINENUM>
  <NAME> Line 1 </NAME>
  <DATE> 2009-01-01 </DATE>
 <ExtraCol1> Col1 </ExtraCol1>
 <ExtraCol2> Col1 </ExtraCol2>
 <ExtraCol3> Col1 </ExtraCol3>
 <ExtraCol4> Col1 </ExtraCol4>
</POLINE>

<POLINE> 
  <POLINENUM> Line2 </POLINENUM>
  <NAME> Line 2 </NAME>
  <DATE> 2011-11-21 </DATE>
 <ExtraCol1> Col1 </ExtraCol1>
 <ExtraCol2> Col1 </ExtraCol2>
 <ExtraCol3> Col1 </ExtraCol3>
 <ExtraCol4> Col1 </ExtraCol4>
</POLINE>

<POLINE> 
  <POLINENUM> Line3 </POLINENUM>
  <NAME> Line 3 </NAME>
  <DATE> 2012-01-07 </DATE>
 <ExtraCol1> Col1 </ExtraCol1>
 <ExtraCol2> Col1 </ExtraCol2>
 <ExtraCol3> Col1 </ExtraCol3>
 <ExtraCol4> Col1 </ExtraCol4>
</POLINE>


</PO>
</ROOT>

Now I need xslt transformation to get some selected fields out of it and the finally get the result in .NET Class object for further processing:

C# Classes as below:

public class PO
{
    public string PONUM { get; set; }
    public string PODESCRIPTION { get; set; }

    public List<POLINES> POLines { get; set; }

}

public class POLINES
{

    public string POLINENUM { get; set; }
    public string LINEDESCRIPTION { get; set; }
}

So finally in the PO class object I need PONUM = "A101" PODESCRIPTION= "THis is sample PO"

And then three POLINES in the public List POLines { get; set; }

Cheers

我没有多少经验,XSLT转换想也许我可以在这里得到一些帮助。

我从我收到这个大通过MSMQ的XML字符串。NET程序。让我们举例如下:

<ROOT>
<PO>
  <PONUM> A101 </PONUM>
  <DESCRIPTION> THis is sample  PO </DESCRIPTION>
  <ADDRESS> Addresss here </ADDRESS>
  <ExtraInfo1> Any Info </ExtraInfo1>
  <ExtraInfo2> Any Info </ExtraInfo2>

<POLINE> 
  <POLINENUM> Line1 </POLINENUM>
  <NAME> Line 1 </NAME>
  <DATE> 2009-01-01 </DATE>
 <ExtraCol1> Col1 </ExtraCol1>
 <ExtraCol2> Col1 </ExtraCol2>
 <ExtraCol3> Col1 </ExtraCol3>
 <ExtraCol4> Col1 </ExtraCol4>
</POLINE>

<POLINE> 
  <POLINENUM> Line2 </POLINENUM>
  <NAME> Line 2 </NAME>
  <DATE> 2011-11-21 </DATE>
 <ExtraCol1> Col1 </ExtraCol1>
 <ExtraCol2> Col1 </ExtraCol2>
 <ExtraCol3> Col1 </ExtraCol3>
 <ExtraCol4> Col1 </ExtraCol4>
</POLINE>

<POLINE> 
  <POLINENUM> Line3 </POLINENUM>
  <NAME> Line 3 </NAME>
  <DATE> 2012-01-07 </DATE>
 <ExtraCol1> Col1 </ExtraCol1>
 <ExtraCol2> Col1 </ExtraCol2>
 <ExtraCol3> Col1 </ExtraCol3>
 <ExtraCol4> Col1 </ExtraCol4>
</POLINE>


</PO>
</ROOT>

现在我需要XSLT转换得到一些选定的领域了,最后得到的结果进行进一步的处理网类对象:

C #类如下:

public class PO
{
    public string PONUM { get; set; }
    public string PODESCRIPTION { get; set; }

    public List<POLINES> POLines { get; set; }

}

public class POLINES
{

    public string POLINENUM { get; set; }
    public string LINEDESCRIPTION { get; set; }
}

So finally in the PO class object I need PONUM = "A101" PODESCRIPTION= "THis is sample PO"

And then three POLINES in the public List POLines { get; set; }

干杯

answer1: 回答1:

No need XSLT. Use XmlSerializer. It simple and short.

PO po;
var xs = new XmlSerializer(typeof(PO));

using (var stringReader = new StringReader(xml))
using (var xmlReader = XmlReader.Create(stringReader))
{
    xmlReader.ReadToFollowing("PO");
    po = (PO)xs.Deserialize(xmlReader);
}

Your classes should look like:

public class PO
{
    public string PONUM { get; set; }
    [XmlElement("DESCRIPTION")]
    public string PODESCRIPTION { get; set; }
    [XmlElement("POLINE")]
    public List<POLINES> POLines { get; set; }
}

public class POLINES
{
    public string POLINENUM { get; set; }
    [XmlElement("NAME")]
    public string LINEDESCRIPTION { get; set; }
}

Which xml node must conform to the LINESESCRIPTION property? I have written NAME in the attribute. Change it to the right.

不需要XSLT。使用XmlSerializer。它简单而短暂。

PO po;
var xs = new XmlSerializer(typeof(PO));

using (var stringReader = new StringReader(xml))
using (var xmlReader = XmlReader.Create(stringReader))
{
    xmlReader.ReadToFollowing("PO");
    po = (PO)xs.Deserialize(xmlReader);
}

你的课应该看起来像:

public class PO
{
    public string PONUM { get; set; }
    [XmlElement("DESCRIPTION")]
    public string PODESCRIPTION { get; set; }
    [XmlElement("POLINE")]
    public List<POLINES> POLines { get; set; }
}

public class POLINES
{
    public string POLINENUM { get; set; }
    [XmlElement("NAME")]
    public string LINEDESCRIPTION { get; set; }
}

XML节点必须符合linesescription财产吗?我在属性中写了名字。把它改到右边。

c#  .net  xml  xslt