Správičky 2 174 Blogy 553 Fórum 13 339

linq xml

photo
jakub
12. 3. 2010 12:23:06
Body: 3625
Najaktívnejší č.: 8

linq xml

ako vytiahnut z xml <productcategoryitem>
<Products>
<ProductItem>
<ProductCode>1024</ProductCode>
<ProductCategories>
<ProductCategoryItem>xxx&gt;Foto a video&gt;Čistenie a údržba&gt;Ochrana displeja</ProductCategoryItem>
</ProductCategories>
</ProductItem>
var tutorials = from tutorial in xmlDoc.Descendants("Products")
                            select
new
                            
{
                                a
= tutorial.Element("productCode").Value,
                                b
= tutorial.Element("productcategoryitem").Value, //tu je chyba
                              

                            }
;


jakub

[Reakcia]

photo
T
12. 3. 2010 13:27:38
Body: 15925
Najaktívnejší č.: 2

RE: linq xml

var products=
from
node
in xDoc.Element("Products").Elements("ProductItem")
select
new
{
Code
= node.Element("ProductCode").Value,
Categories
=
(
from
category
in node.Elements("ProductCategories").Elements("ProductCategoryItem")
select
category.Value
).ToList()
}
;

Tomáš Zeman, MCSD.NET

[Reakcia]

photo
jakub
12. 3. 2010 15:28:37
Body: 3625
Najaktívnejší č.: 8

RE: linq xml

snazim sa nacitat zaznamy z xml suboru do tabulky no berie iba prvy
zaznam aj ked by malo nacitat
2 zaznamy, ide o subor ,ktory ma az 26 MB teda asi 10000 zaznamov a tieto potrebujem importnut z xml do tabulky mssql


public class Tutorial
       
{
           public
string a { get; set; }
          
// public string b { get; set; }
          // public decimal c { get; set; }
       }


       public
void Button3_Click(object sender, System.EventArgs e)
       
{


           try
           
{



           string soubor1
=
Server.MapPath(
"~/data/zhraj/xml/testa.xml");//
Server.MapPath("test.xml");
           XDocument xmlDoc
= XDocument.Load(soubor1);
           string txttconn
=
System.Configuration.ConfigurationManager.AppSettings[
"myconnection"];


           var tutorials
= from tutorial in xmlDoc.Descendants("PriceList")
                           select
new
                           
{
                               a
=
tutorial.Element(
"Products").Element("ProductItem").Element("ProductName").Value,


                           }
;

           SqlConnection conn
= new SqlConnection(txttconn);
           conn.Open();

           foreach (var tutorial
in tutorials)
           
{

               Response.Write(
"a");
               var p
= tutorial.a;
             
/* var k = tutorial.b;
               var d = tutorial.c;
*/

               Response.Write(p);
               SqlCommand cmd
= new SqlCommand("INSERT INTO TESTA<br />(name) values (@a)", conn);<br />               SqlParameterCollection sqlParams = cmd.Parameters;
               sqlParams.Add(
@"a", SqlDbType.VarChar).Value=p;

               cmd.ExecuteNonQuery();
           }
;
           conn.Close();
           }
</span>
xml version="1.0" encoding="Utf-8" ?>
- <PriceList version="1.0" application="Shop" note="" xmlns="">
- <Products>
- <ProductItem>
  
<ProductCode>1024</ProductCode>
  
<ProductName>1024 Display Protection Foil Displex Protector, 3.0</ProductName>
  
<ProductType>HAMA</ProductType>
  
<Availability>DE</Availability>
  
<PriceDmoc>10,84</PriceDmoc>
  
<Price>5,85</Price>
  
<MinimalOrder>0</MinimalOrder>
  
<Bestseller>False</Bestseller>
  
<News>False</News>
  
<Bargain>False</Bargain>
  
<Discount>False</Discount>
  
<Action>False</Action>
  
<PriceUp>False</PriceUp>
  
<NoDiscount>False</NoDiscount>
  
<ProductAlternates />
- <ProductCategories>
  
<ProductCategoryItem>HAMA>Foto a video>Čistenie a údržba>Ochrana displeja</ProductCategoryItem>
  
</ProductCategories>
  
<ProductInventory>0</ProductInventory>
  
<ProductEan>4007249010247</ProductEan>
  
<ProductHeight>23,40</ProductHeight>
  
<ProductWidth>0,20</ProductWidth>
  
<ProductLength>9,60</ProductLength>
  
<ProductWeight>15,500</ProductWeight>
  
<ProductPackQuantity>25</ProductPackQuantity>
  
<MasterCartonHeight>20,00</MasterCartonHeight>
  
<MasterCartonWidth>40,00</MasterCartonWidth>
  
<MasterCartonLength>57,00</MasterCartonLength>
  
<MasterCartonWeight>7750,000</MasterCartonWeight>
  
<MasterCartonQuantity>500,00</MasterCartonQuantity>
  
<PalletQuantity>3000</PalletQuantity>
- <ProductPictures>
- <ProductPictureItem>
  
<ProductPictureMain>http://www.hama.sk/ProductImages/00001/abs/00001024abs.jpg
  <ProductPictureThumb>http://www.hama.sk/ProductImages/00001/abt/00001024abt.jpg
  <ProductPictureDetail>http://www.hama.sk/ProductImages/00001/abb/00001024abb.jpg
  </ProductPictureItem>
  
</ProductPictures>
  
</ProductItem>
- <ProductItem>
  
<ProductCode>1052</ProductCode>
  
<ProductName>1052 DIARÁMČEKY DSR 1,6 200 KS</ProductName>
  
<ProductType>HAMA</ProductType>
  
<Availability>DE</Availability>
  
<PriceDmoc>23,36</PriceDmoc>
  
<Price>13,00</Price>
  
<MinimalOrder>0</MinimalOrder>
  
<Bestseller>False</Bestseller>
  
<News>False</News>
  
<Bargain>False</Bargain>
  
<Discount>False</Discount>
  
<Action>False</Action>
  
<PriceUp>False</PriceUp>
  
<NoDiscount>False</NoDiscount>
  
<ProductAlternates />
- <ProductCategories>
  
<ProductCategoryItem>HAMA>Foto a video>Dia>Dia rámčeky</ProductCategoryItem>
  
</ProductCategories>
  
<ProductInventory>0</ProductInventory>
  
<ProductEan>4007249010520</ProductEan>
  
<ProductHeight>16,50</ProductHeight>
  
<ProductWidth>5,50</ProductWidth>
  
<ProductLength>10,30</ProductLength>
  
<ProductWeight>502,000</ProductWeight>
  
<ProductPackQuantity>1</ProductPackQuantity>
  
<MasterCartonHeight>16,60</MasterCartonHeight>
  
<MasterCartonWidth>27,50</MasterCartonWidth>
  
<MasterCartonLength>52,00</MasterCartonLength>
  
<MasterCartonWeight>12878,000</MasterCartonWeight>
  
<MasterCartonQuantity>25,00</MasterCartonQuantity>
  
<PalletQuantity>750</PalletQuantity>
- <ProductPictures>
- <ProductPictureItem>
  
<ProductPictureThumb>http://www.hama.sk/ProductImages/00001/abt/00001052abt.jpg
  <ProductPictureDetail>http://www.hama.sk/ProductImages/00001/abb/00001052abb.jpg
  </ProductPictureItem>
  
</ProductPictures>
  
</ProductItem>
  
</Products>
  
</PriceList>

jakub

[Reakcia]

photo
T
12. 3. 2010 20:38:30
Body: 15925
Najaktívnejší č.: 2

RE: linq xml

Ahoj,

je to preto, lebo
xmlDoc.Descendants("PriceList") je prave jeden node, nie zoznam.


Pozri si moj priklad.

rychly fix toho Tvojho kodu je takyto

xmlDoc.Descendants("ProductItem") a potom len Element("ProductName").Value

robis len jednorazovy import?

Tomáš Zeman, MCSD.NET

[Reakcia]



Najaktívnejší užívatelia
1. 34425 b. photo vlko
2. 15925 b. photo T
3. 15330 b. photo spigi
4. 5885 b. photo dudok
5. 5510 b. photo slavof
6. 4935 b. photo siro
7. 4715 b. photo duracellko
8. 3625 b. photo jakub
9. 3490 b. photo lubolacko
10. 3340 b. photo skippo