creating XML file with LINQ to XML

安稳与你 提交于 2019-12-24 19:07:55

问题


I need to write linq to xml query of such kind of below xml data

<PRODUCT>
<ID>34169</ID>
<D1>good</D1>
<D2>well</D2>
<L1>lame</L1>
<L2>killer</L2>
<BR>IOMEGA</BR>
<KDV>18</KDV>
<IMG>34169.JPG</IMG>
<EAN>data1</EAN>
<ATP>50+</ATP>
<DM3>0,51</DM3>
<S>
  <L>Tip</L>
  <V>HARICI</V>
</S>
<S>
  <L>Renk</L>
  <V>METALIK GRI</V>
</S>
<S>
  <L>Kapasite (GB)</L>
  <V>500</V>
</S>
<S>
  <L>Dönüş Hızı (Rpm)</L>
  <V>5400,0</V>
</S>
<S>
  <L>Arabirim</L>
  <V>USB 2.0</V>
</S>
<S>
  <L>Form Faktörü (Inch)</L>
  <V>2,5</V>
</S>
<S>
  <L>Ön Bellek (Kb/mb)</L>
  <V>8,0</V>
</S>
<S>
  <L>Satış Garanti Süresi (ay)</L>
  <V>36</V>
</S>

my problems are

  1. get only the first image when there is several images in the database(img holds the path of an image as a string in my database table)

  2. handle the <S> part

I am new to LINQ to XML please help, thanks.


回答1:


Hope this helps..

        var productElement = XDocument.Load("product.xml").Root;

        var firstImagePath  = productElement.Element("IMG").Value;
        var sElements = productElement.Elements("S");

        //if you want an object instead of XElements, you can do
        var sElementObjects = sElements.Select(xe => new
                                        {
                                              L = xe.Element("L").Value,
                                              V = xe.Element("V").Value,
                                         });



回答2:


there is my answer

 XElement productCatalog = 
                   new XElement("PRODUCTCATALOG",

                               from urun in db.TBLP1URUNs
                               select new XElement("PRODUCT",

                                           new XElement("ID", urun.ID),

                                           new XElement("D1", urun.MODEL),

                                           new XElement("D2", urun.URUNACIKLAMA),

                                           new XElement("L1", urun.TBLP1URUNKATEGORI.TREENAME),

                                           new XElement("IMG", db.TBLP1URUNRESIMs.Where(p => p.URUN_ID == urun.ID).First().FILE_NAME),

                                           new XElement("EAN", urun.STOKKODU),

                                           new XElement("ATP", GetUrunStokById(urun.ID)),//STOKMIKTARI

                                           new XElement("DM3", urun.DESI),

                                           from ozellikler in urun.TBLP1OZELLIK_URUNs
                                           select new XElement("S",

                                               new XElement("L", ozellikler.TBLP1OZELLIK.TBLP1OZELLIKTIPI.TIPI),

                                               new XElement("V", ozellikler.TBLP1OZELLIK.OZELLIK))

                                               )                           
                            );


来源:https://stackoverflow.com/questions/5632747/creating-xml-file-with-linq-to-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!