Forum

C# ile xml attribut...
 
Bildirimler
Hepsini Temizle

C# ile xml attribute okumada yardım.

4 Yazılar
3 Üyeler
0 Reactions
853 Görüntüleme
(@Anonim)
Gönderiler: 0
Konu başlatıcı
 

  <Sport id="23" name="Basketbol">
    <Country id="15" name="A.B.D.">
      <Tournament id="9" name="Amerikan Basketbol Ligi" type="1" typeDesc="League">
        <Season id="95" name="2010/2011">
          <Stage id="777" name="2010/2011 Sezonu">
            <Team id="104" name="Chicago Bulls" conferenceId="3" conference="Merkez">
              <General position="1" totalPlayed="82" won="62" draw="0" lost="20" goalsScored="8087" goalsAgainst="7485" points="144" form="W- W- W- W- W- W" />
              <Home position="1" totalPlayed="41" won="36" draw="0" lost="5" goalsScored="4104" goalsAgainst="3683" points="77" />
              <Away position="4" totalPlayed="41" won="26" draw="0" lost="15" goalsScored="3983" goalsAgainst="3802" points="67" />
            </Team>

 

bu şekilde gelen bir xm verim var benim bunları 

<sport>
<id>23></id>
<name>Basketbol</name>
<Country>
<id>15</id>
<name>A.B.D.</name>

şeklinde yine xml dosyası olarak kaydetmeliyim bu konuda şimdiden teşekkürler.

 
Gönderildi : 05/05/2011 22:14

(@selahattinsadoglu)
Gönderiler: 826
Prominent Member
 

Merhaba,

Şu ana kadar denemiş olduğunuz kod nedir?

 
Gönderildi : 05/05/2011 23:57

(@selahattinsadoglu)
Gönderiler: 826
Prominent Member
 

Merhaba,

 

Kod olarak aşağıdaki gibi bir şey olabilir.

using System.Xml.Linq;
// [...]   

var sourceDoc = XDocument.Load(@"C:\Your\Source\Xml\File.xml");
var sports = sourceDoc.Descendants().Where(s => s.Name == "Sport");

var destDoc = new XDocument(sports.Select(s =>
    new XElement("sport",
        new XElement("id", s.Attribute("id").Value),
        new XElement("name", s.Attribute("name").Value),
        new XElement("country",
            new XAttribute("id", s.Descendants().Where(c => c.Name == "Country").FirstOrDefault().Attribute("id").Value),
            new XAttribute("name", s.Descendants().Where(c => c.Name == "Country").FirstOrDefault().Attribute("name").Value)
        )
    )
));

destDoc.Save(@"C:\Your\Destination\Xml\File.xml");

 

Saygılar,

 
Gönderildi : 07/05/2011 20:15

(@unalaygun)
Gönderiler: 8
Eminent Member
 

Arkadaşım sana daha önce kullandığımız gelen veriyi xml olarak kaydeden bir kod parçacığı atıyorum umarım işine yarar.

İyi çalışmalar.

 

  for (int i = 0; i < gridStyleDs.Tables[0].Rows.Count; i++)
                        {
                            try
                            {
                                uc = i;

                                XmlNode root = doc.CreateNode(XmlNodeType.Element, gridStyleDs.Tables[0].Rows[i]["KolonAdi"].ToString().Trim(), "");

                                XmlElement elem;
                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["IsHidden"].ToString()))
                                {
                                    elem = doc.CreateElement("Hidden");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["IsHidden"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["Caption"].ToString()))
                                {
                                    elem = doc.CreateElement("Caption");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["Caption"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["Width"].ToString()) &&
                                    Convert.ToInt32(gridStyleDs.Tables[0].Rows[i]["Width"].ToString()) != 0)
                                {
                                    elem = doc.CreateElement("Width");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["Width"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["CellActivation"].ToString()))
                                {
                                    elem = doc.CreateElement("CellActivation");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["CellActivation"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["CultureModule"].ToString()))
                                {
                                    elem = doc.CreateElement("CultureModule");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["CultureModule"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["DataType"].ToString()))
                                {
                                    elem = doc.CreateElement("DataType");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["DataType"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["DefaultValue"].ToString()))
                                {
                                    elem = doc.CreateElement("DefaultCellValue");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["DefaultValue"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["Format"].ToString()))
                                {
                                    elem = doc.CreateElement("Format");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["Format"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["IsAutoResize"].ToString()) &&
                                    Convert.ToBoolean(gridStyleDs.Tables[0].Rows[i]["IsAutoResize"].ToString()) == true)
                                {
                                    elem = doc.CreateElement("Width");
                                    elem.InnerText = "AutoResize";

                                    root.AppendChild(elem);
                                }

                                if (!string.IsNullOrEmpty(gridStyleDs.Tables[0].Rows[i]["Style"].ToString()))
                                {
                                    elem = doc.CreateElement("Style");
                                    elem.InnerText = gridStyleDs.Tables[0].Rows[i]["Style"].ToString().Trim();

                                    root.AppendChild(elem);
                                }

                             

                                rootNode.AppendChild(root);

                                doc.Save(path + "\\" + modulDs.Tables[0].Rows[m]["ModulAdi"].ToString() + "\\" + styleDs.Tables[0].Rows[l]["StyleAdi"]);
                              
                            }
                            catch (Exception ex)
                            {
                                errorLog = path + "\\" + modulDs.Tables[0].Rows[bir]["ModulAdi"].ToString() + "\\" +
                                    styleDs.Tables[0].Rows[iki]["StyleAdi"] + "\\" +
                                                gridStyleDs.Tables[0].Rows[uc]["KolonAdi"].ToString() + "[ObjId] = " + gridStyleDs.Tables[0].Rows[uc]["ObjId"].ToString();
                                listBox1.Items.Add(errorLog+"\n"+ex.Message  );
                                Application.DoEvents();
                               

                            }
                        }

 
Gönderildi : 06/06/2011 12:14

Paylaş: