0
This is XML Document.I want to bind XML data to GridView using Linq to XML .I want data from title_rec element.There are near about 94 records.  :-
  <?xml version="1.0" encoding="UTF-8" ?>
 <dtg>
 <search_header>
 <plant_currency>
 <thru_date>
  <year>2012</year>
  <month>11</month>
  <day>15</day>
  </thru_date>
 <thru_time>
  <hour>05</hour>
  <minute>00</minute>
  <ampm>PM</ampm>
  <timezone>PST</timezone>
  </thru_time>
  </plant_currency>
 <search_date>
  <year>2012</year>
  <month>11</month>
  <day>19</day>
  </search_date>
 <search_time>
  <hour>01</hour>
  <minute>04</minute>
  <ampm>PM</ampm>
  <timezone>PST</timezone>
  </search_time>
  </search_header>
 <search_object type="PROPERTY" outcome="SUCCESS">
 <accepted_param>
 <title_param>
 <property_legal type="TITLE_BY_APN">
 <apn>
  <book>277</book>
  <page>312</page>
  <parcel>003</parcel>
  </apn>
  <QS n="XP" v="XA277}312 003" l="T" />
  </property_legal>
 <from_date>
  <year>1980</year>
  <month>05</month>
  <day>31</day>
  </from_date>
 <thru_date>
  <year>2011</year>
  <month>05</month>
  <day>31</day>
  </thru_date>
  <qualifier extended_names="YES" tt_options="SAP" />
  </title_param>
  </accepted_param>
 <search_unit type="PROPERTY" report_occurrence="01" report_count="03" outcome="SUCCESS">
 <search_param>
 <title_param>
 <property_legal type="TRACT">
 <plat type="TRACT">
 <tract>
  <number>13897</number>
  <id>1</id>
  </tract>
  </plat>
  <lot>85</lot>
 <apn>
  <book>277</book>
  <page>312</page>
  <parcel>003</parcel>
  </apn>
  <QS n="SL" v="T 13897 1$[85" l="L" />
  </property_legal>
 <from_date>
  <year>1980</year>
  <month>05</month>
  <day>31</day>
  </from_date>
 <thru_date>
  <year>2011</year>
  <month>05</month>
  <day>31</day>
  </thru_date>
  <qualifier extended_names="YES" tt_options="SAP" />
  </title_param>
  </search_param>
 <filters>
 <filter type="SORT">
 <path collate="ASCENDING">datatrace/response/search_group/search_object/search_unit/title_report/title_rec/date</path>
  </filter>
  </filters>
  <search_desc command="T" type="Tract" short="L:85 T:138971" long="Lot: 85 Tract: 138971" />
 <title_report record_count="91">
 <property_legal type="TRACT">
 <plat type="TRACT">
 <tract>
  <number>13897</number>
  <id>1</id>
  </tract>
  </plat>
  <lot>85</lot>
  <QS n="SL" v="T 13897 1$[85" l="L" />
  <message>PLAT HAS NOT BEEN VERIFIED</message>
  </property_legal>
  <section_index section_id="1" />
 <title_rec section_id="1">
 <date>
  <year>1980</year>
  <month>11</month>
  <day>03</day>
  </date>
  <seq>1</seq>
 <base>
 <base_date>
  <year>1980</year>
  <month>11</month>
  <day>03</day>
  </base_date>
 <property>
 <property_legal type="TRACT">
 <plat type="TRACT">
 <tract>
  <number>13897</number>
  <id>1</id>
  </tract>
  </plat>
  <lot>85</lot>
  <QS n="SL" v="T 13897 1$[85" l="L" />
  </property_legal>
  </property>
 <company>
  <number>08</number>
  </company>
 <order>
  <number>0052337</number>
  <tof>04</tof>
  </order>
 <inst>
  <type>SR</type>
 <recorded>
  <year>1980</year>
  <month>11</month>
  <day>03</day>
  </recorded>
 <posted>
  <year>1980</year>
  <month>11</month>
  <day>03</day>
  </posted>
  </inst>
  </base>
  </title_rec>
 <title_rec section_id="1">
 <date>
  <year>1980</year>
  <month>11</month>
  <day>10</day>
  </date>
  <seq>2</seq>
 <base>
 <base_date>
  <year>1980</year>
  <month>11</month>
  <day>10</day>
  </base_date>
 <property>
 <property_legal type="TRACT">
 <plat type="TRACT">
 <tract>
  <number>13897</number>
  <id>1</id>
  </tract>
  </plat>
  <lot>85</lot>
  <QS n="SL" v="T 13897 1$[85" l="L" />
  </property_legal>
  </property>
 <company>
  <number>08</number>
  </company>
 <order>
  <number>0052337</number>
  <tof>04</tof>
  </order>
 <inst>
  <type>SR</type>
 <recorded>
  <year>1980</year>
  <month>11</month>
  <day>10</day>
  </recorded>
 <posted>
  <year>1980</year>
  <month>11</month>
  <day>10</day>
  </posted>
  </inst>
  </base>
  </title_rec>
 <title_rec section_id="1">
 <date>
  <year>1980</year>
  <month>11</month>
  <day>13</day>
  </date>
  <seq>3</seq>
 <base>
 <base_date>
 <year>1980</year>
  <month>11</month>
 <day>13</day>
  </base_date>
 <property>
 <property_legal type="TRACT">
 <plat type="TRACT">
 <tract>
  <number>13897</number>
  <id>1</id>
  </tract>
  </plat>
  <lot>85</lot>
  <QS n="SL" v="T 13897 1$[85" l="L" />
  </property_legal>
  </property>
 <company>
  <number>08</number>
  </company>
 <order>
  <number>0052600</number>
  <tof>04</tof>
  </order>
 <inst>
  <type>SR</type>
 <recorded>
  <year>1980</year>
  <month>11</month>
  <day>13</day>
  </recorded>
 <posted>
  <year>1980</year>
  <month>11</month>
  <day>13</day>
  </posted>
  </inst>
  </base>
  </title_rec>
 <title_rec section_id="1">
 <date>
  <year>1980</year>
  <month>11</month>
  <day>25</day>
  </date>
  <seq>4</seq>
 <title_doc>
 <property>
 <property_legal type="TRACT">
<plat type="TRACT">
 <tract>
  <number>13897</number>
  <id>1</id>
  </tract>
  </plat>
  <lot>85</lot>
  <QS n="SL" v="T 13897 1$[85" l="L" />
  </property_legal>
  </property>
 <inst>
  <year>1980</year>
  <number>220434</number>
  <type>RL</type>
 <recorded>
  <year>1980</year>
  <month>11</month>
  <day>25</day>
  </recorded>
 <posted>
  <year>1980</year>
  <month>11</month>
  <day>25</day>
  </posted>
 <image provider="DATA_TREE" county_fips="065" document_id_desc="DocumentYear.DocID">
 <image_params document_type="DAILY_DOCUMENT" index_type="YEAR.DOCID" document_index_id="16">
  <description>1980.220434</description>
  </image_params>
  </image>
  <QS n="IN" v="Y1980220434" l="B" />
  </inst>
 <party_info>
 <party role="GRANTOR">
  <name>RECORD OWNER</name>
  <QS n="GN" v="RECORD OWNER" l="N" />
  </party>
  </party_info>
 <remarks>
  <len>9</len>
  <text>80 139134</text>
  </remarks>
  </title_doc>
  </title_rec>
  </title_report>
 <commentary>
 <comment>
  <text>ADJOINING PROPERTY SEARCH SUPPRESSED</text>
  </comment>
 <comment>
  <text>ALL TRACT SEARCH SUPPRESSED</text>
  </comment>
  </commentary>
  </search_unit>
  </search_object>
  </dtg>

我创建了 TitleByApn 类并编写了 Linq to XML Queries 来解析 xml 数据。请在下面找到类详细信息和返回查询输出的代码。请使用此代码,或者如果您可以提供一些具有不同逻辑的其他代码也可以。

public class TitleByApn
{
 private string grantor;
 private string grantee;
 private string doctype;
 private string instrument;
 private string month;
 private string  day;
 private string  year;
 private string remarks;

 public string _grantor
{  get { return grantor; }
}

public string _grantee
{
 get {return grantee ;}
}

public string _month
{
 get {return month; }
}

public string _day
{
 get {return day; }
}

public string _year
{
 get { return year; }
} 

public string _doctype
{
get{ return doctype; }
}

public string  _remarks
{
get {return remarks;}
}

public string  _instrument
{
get {return instrument; }
}

public static List<TitleByApn> GetTitle()


        {

            List<TitleByApn> output = new List<TitleByApn>();
            string filepath = @"C:/Title.xml";

            //XElement xdoc = new XElement();

              XElement xdoc =   XElement.Load(filepath);

            //XElement xElement = XElement.Parse(filepath);

            //var query = from b in xdoc.Descendants("title_doc")
            //            select b.Value;

            //string query = (from b in xdoc.Descendants("title_doc")
            //                select b.Value).FirstOrDefault();

            int i = 0;
            string s1 = "";
            string inst = "";
            //string s2 = "";
            //string s3 = "";
            //string s4 = "";
            var authorList = xdoc.Descendants("title_doc").Select(x => x.Value).ToList();
            foreach (string item in authorList)
            {


                     string Month = (from o in xdoc.Descendants("title_doc").Descendants("recorded")
                                 select o.Element("month").Value).First();

                string Day = (from o in xdoc.Descendants("title_doc").Descendants("recorded")
                               select o.Element("day").Value).First();

                string Year = (from o in xdoc.Descendants("title_doc").Descendants("recorded")
                                select o.Element("year").Value).First();

                string granTor = (from o in xdoc.Descendants("title_doc").Descendants("party_info").Descendants("party")
                                   where o.Attribute("role").Value == "GRANTOR"
                                  select o.Element("name").Value).FirstOrDefault();
                ////select new Instrument()

                ////{
                ////    Grantor = (string)o.Element("name")

                ////};

                string granTee = (from o in xdoc.Descendants("title_doc").Descendants("party_info").Descendants("party")
                                   where o.Attribute("role").Value == "GRANTEE"
                                  select o.Element("name").Value).First();

                string Doctype =(from o in xdoc.Descendants("title_doc").Descendants("inst")
                                   select o.Element("type").Value).First();

                 string instNo = (from o in xdoc.Descendants("title_doc").Descendants("inst")
                                   select o.Element("number").Value).First();

                  string REmarks = (from o in xdoc.Descendants("title_doc").Descendants("remarks")
                                   select o.Element("text").Value).First();

               output.Add(new TitleByApn(granTor, granTee, Month, Day, Year,instNo, Doctype, REmarks));


                }


            return output;
             } 

}

This is the code responsible to bind the class to gridview:
GridView1.DataSource = TitleByApn.GetTitle();
            GridView1.DataBind();


I know because of FirstOrDefault() only first record is displaying in gridview.
I want code to display all the records in gridview.
Please help me ASAP.
Thanks in advance.
4

1 回答 1

0

根据您问题的内容,听起来您已经解析了 XML 并将其放入您在上面创建的类中。本教程在 Codeplex 上找到,标题为“详细的数据绑定教程 ”,非常详细地介绍了如何将您的类绑定到带有访问修饰符等的 DataGridView。

本质上,一旦你创建了你的 GridView,你可以添加一个 BindingSource 对象到它的数据项中,然后简单地添加你的 XML 类信息:

private void Form1_Load(object sender, EventArgs e)
{
 airplaneBindingSource.Add(new Airplane("Boeing 747", 800));
  airplaneBindingSource.Add(new Airplane("Airbus A380", 1023));
  airplaneBindingSource.Add(new Airplane("Cessna 162", 67));
}
于 2013-02-01T20:40:28.363 回答