开发者

How to Retrieve data and want to read data from XML in C#

I am using windows application and I have four combo boxes(comboeventname,combosendtype,comboType,comboschedule) in that form.... I have stored that combo values in to XML file by using XML writer... Now I want to display the data in that combo boxes in form load event when form opens in run time... How to retrieve that values from that XML file and how to display that data in combo boxes whi开发者_高级运维le run time? How shall I do this?

Anyone tell me the solution of this.....

Thanks in Advance...


We can probably get XmlReader working if you show the xml or the code you used to write it, but I'm not sure that is the best option here. Presumably, to display them in a combobox the data-volume isn't immense. In that case, it would be much simpler to use any of:

  • XmlDocument
  • XDocument
  • XmlSerializer

etc to load the data into a DOM or object-model, and work from their. LINQ-to-XML (via XDocument) may be particularly appealing. For example, with the xml:

<options>
  <option value='123'>ABC</option>
  <option value='234'>DEF</option>
  <option value='567'>GHI</option>
</options>

The XDocument code like below may work:

var options =
      from option in XElement.Parse(xml).Elements("option")
      select new {
         value = (int)option.Attribute("value"),
         text = option.Value
      };


Probably much easier to use XmlDocument , or XDocument.

XmlDocument: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.selectsinglenode.aspx

private void Form1_Load(object sender, EventArgs e)
        {
            //load the xml document;

            XmlDocument xdoc = new XmlDocument;
            xdoc.Load("YourFile.xml");


            // read the values

            // using indexers
            method1 = xdoc["root"]["Element"].Value;

            // using xpath to select nodes
            method2 = xdoc.SelectSingleNode( "root/element/element" ).Value;

            // attributes
            method3 = xdoc.SelectSingleNode("root/element").Attributes["YourAttribute"].Value;

        }

XmlReader is better used for large XML files, 1000s of elements, where you don't want to load the whole document into memory. Your document sound far to small to use XmlReader.


I'd suggest using an XmlReader. There's a lot of documentation around but here's a start:

http://msdn.microsoft.com/en-us/library/9d83k261%28VS.80%29.aspx

Once you have the data you can add them to the your form controls.

Alternatively you could use an XmlDocument - although it does not perform as well as the XmlReader I doubt you will notice with this situation.


I have used before xsd mappings to generate class mappings for an xml document. On visual studio command prompt line use xsd commands like below . This will generate mapping class and then you should deserialize xml file to object and cast to generated mapping class.

xsd "path of xml file" this genarates xsd file

than command promt again xsd "pat of generated xsd file" /CLASSES

for details look at that sample

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜