วันศุกร์ที่ 31 สิงหาคม พ.ศ. 2561

WEEK 3 : C# Parser



หน้าตาของโปรแกรมที่ได้ เขียนขึ้นมาเพื่อ ทดลองใช้งาน C# XML Parser

โดยจะใช้งานในส่วนของ

using System.Xml.Linq;

โดยในส่วนของ ข้อมูล XML นั้นจะมีโครงสร้างดังนี้

<?xml version="1.0" encoding="utf-8"?> 
<breakfast_menu> 
   <food>    
     <name>Belgian Waffles</name>    
     <price>5.95</price>    
     <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>         <calories>650</calories>  
  </food> .
 . .

โดย เริ่มจากการกดปุ่มเลือกไฟล์เพื่อโหลดแล้วจะได้ผลลัพธ์ดังนี้




โดยโค้ดที่ควบคุมในนี้ส่วนนี้คือ
private XElement xmlFileLinq;

this.xmlFileLinq = XElement.Load(this.path);

his.output_box.Text += this.xmlFileLinq;

ใช้ . Load(path) เพื่อโหลด มาอ่าน จากนั้น สามารถ นำมาแสดงผลได้เลย Linq มีการจัดการ รูปแบบการแสดงผล ให้อยู่แล้ว


ในส่วนของการเพิ่มข้อมูล จะผ่านโค้ดดังนี้

XElement newElement = new XElement("food",                                          
  new XElement("name", this.input_name.Text),                                           
  new XElement("price", this.input_price.Text),                                          
  new XElement("description", this.input_descrip.Text),                                          
  new XElement("calories", this.input_cal.Text));     
       
this.xmlFileLinq.LastNode.AddAfterSelf(newElement);  

โดยทำการสร้าง XElement ขึ้นมาเพื่อรวมข้อมูลก่อน จากนั้น ให้ เลือก node สุ้ท้าย แล้ว ทำการเพิ่ม
ข้อมูลใหม่นั้น หลัง node สุดท้าย

ใช้คำสั่ง .LastNode  และ .AddAdterSelf


จะเห็นได้ว่า ข้อมูลใหม่ ได้ถูกเพิ่มเข้าไปใน Object Xelement แล้ว แต่ในไฟล์ ยังไม่ได้เซฟ ดังนั้น ต่อ
มา วิธีการ บันทึกนั้น จะมีโค้ดดังนี้


   this.xmlFileLinq.Save(this.path, SaveOptions.None);

ใช้คำสั่ง .Save(path, option) ได้เลย








ต่อมาในส่วนของการเช็ค XML Schema จะใช้ในส่วนของ

using System.Xml.Schema;

กำหนด markup ดังนี้

this.maskUp =         
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>               
<xsd:element name='breakfast_menu'>               
  <xsd:complexType>               
    <xsd:sequence>                 
      <xsd:element name='food'>                   
        <xsd:complexType>                     
          <xsd:sequence>                     
            <xsd:element name='name' type='xsd:string'/>                     
            <xsd:element name='price' type='xsd:decimal'/>                     
            <xsd:element name='description' type='xsd:string'/>                   
            <xsd:element name='calories' type='xsd:integer'/>                   
          </xsd:sequence>                   
        </xsd:complexType>                 
      </xsd:element>               
   </xsd:sequence>               
  </xsd:complexType>               
</xsd:element>           
</xsd:schema>";

และ

this.schemas = new XmlSchemaSet();
// using System.IO for StringReader;
this.schemas.Add("", XmlReader.Create(new StringReader(this.maskUp)));

สร้าง object schema ขึ้นมาและเพิ่ม markup เข้าไป

XDocument check = new XDocument(this.xmlFileLinq);
bool checked_doc = false;

 check.Validate(this.schemas, (o, er) => {
this.output_box.Text += er.Message;  checked_doc = true; });

โดยต่อมาใช้ Xdocument จะมี คำสั่ง .Validate ซึง จะใช้ schema object นี้ไปตรวจเช็ค และทำตามฟังก์ชั่นด้านใน หาก ผิดพลาด

ซึ่งในส่วนนี้ยังเกิดการผิดพลาดอยู่ 





ขึ้นเตือนว่า The element breakfast_menu has invalid child eleement food ซึ่งยังไม่แน่ใจว่าเพราะเหตุใด

ไม่มีความคิดเห็น:

แสดงความคิดเห็น