Pages

Get data from xml file

 

images Today I decided to write my article about xml , firstly I want to say somethink about my grammar ,this is my first article that I use english so I m sory if I make mistake. :))

*Why we use xml file ?
*because in the real world, computer systems and databases contain data in incompatible formats.XML data is stored in plain text format. This provides a software- and hardware-independent way of storing data.This makes it much easier to create data that different applications can share.

XmlTextReader xmlDocument = new XmlTextReader("MyXmlFile.xml");

                while (xmlDocument.Read())
                {
                    if (xmlDocument.NodeType == XmlNodeType.Element)
                    {
                        switch (xmlDocument.Name)
                        {
                            case "ip":
                                _IpAdres = Convert.ToString(xmlDocument.ReadString());
                                break;
                        }
                    }
                }
                xmlDocument.Close();

Console.WriteLine(IpAdres );

*XmlTextReader xmlDocument = new XmlTextReader("TcpServer.xml");
At this sentence you just defined your stream for reading  xml and set it’s parameter which is about file path .
*while (xmlDocument.Read())  after defined your stream, you can start reading the first line of your file
*xmlDocument.NodeType == XmlNodeType.Element  At this part of code you just verify your node type for sure it not root node. And then you can use switch  to take your node name  .
* when you found  which name you search you could read that line and convert which type you want  same this =>   _IpAdres = Convert.ToString(xmlDocument.ReadString());

1 yorum:

boracik dedi ki...

selam, tüm gece uğraştım fakat nedenini bulamadım. sen bloğunda bundan bahsedince yardımın olabileceğini düşündüm.

bir windows formu kaydedip tekrar yükleyebilmek için xml kullanıyorum. kayıt ederken gayet mükemmel bir xml yaratıyorum fakat okurken sorun yaşıyorum.
okumada "if (xmlDocument.NodeType == XmlNodeType.Element)" satırından sonra switch-case'leri atlıyor.
xml'imin yapısı aşağıdaki örneğe benziyor;



veli


kemal




hasan


mustafa





umarım anlatabilmişimdir.

yardımcı olabilirsen sevinirim.

mail adresim boragok@yahoo.com