วันศุกร์ที่ 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 ซึ่งยังไม่แน่ใจว่าเพราะเหตุใด

วันพุธที่ 22 สิงหาคม พ.ศ. 2561

WEEK2 : XML SCHEMA [ part 1 ]


XML SCHEMA
An XML document with correct syntax is called "Well Formed".
An XML document validated against an XML Schema is both "Well Formed" and "Valid".
XML Schema language is also referred to as XML Schema Definition (XSD).
---------------------------------------------------------------------------------
"note.xsd"

  1. <?xml version="1.0"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  3. targetNamespace="https://www.w3schools.com"
  4. xmlns="https://www.w3schools.com"
  5. elementFormDefault="qualified">

  6. <xs:element name="note">
  7.  <xs:complexType>
  8.    <xs:sequence>
  9.      <xs:element name="to" type="xs:string"/>       
  10.      <xs:element name="from" type="xs:string"/>
  11.      <xs:element name="heading" type="xs:string"/>
  12.      <xs:element name="body" type="xs:string"/>
  13.    </xs:sequence>
  14.  </xs:complexType>
  15. </xs:element>

  16. </xs:schema>
  17. element note ประกอบด้วย element จึงใช้ complexType
---------------------------------------------------------------------------------

<note>
 <date>2015-09-01</date>
 <hour>08:30</hour>
 <to>Tove</to>
 <from>Jani</from>
 <body>Don't forget me this
weekend!</body>
</note>

---------------------------------------------------------------------------------


"note.xml"

<?xml version="1.0"?>

<note
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/xml/note.xsd">
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
</note>
---------------------------------------------------------------------------------


Type , default , fixed , required


xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time

<xs:element name="color" type="xs:string" default="red"/>
<xs:element name="color" type="xs:string" fixed="red"/>
<xs:attribute name="lang" type="xs:string" use="required"/>

Restrictions ตัวอย่าง ค่าสูงสุดต่ำสุด .
  1. <xs:restriction base="xs:integer">
  2.      <xs:minInclusive value="0"/>
  3.      <xs:maxInclusive value="120"/> </xs:restriction>
รับเฉพาะ string ที่กำหนด

  1. <xs:restriction base="xs:string">
  2.   <xs:enumeration value="Audi"/>
  3.      <xs:enumeration value="Golf"/>
  4.      <xs:enumeration value="BMW"/>
  5. </xs:restriction>
  6.    <xs:restriction base="xs:string">
  7.      <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
   </xs:restriction>

และยังมีข้อจำกัดอื่นๆ ที่สามารถกำหนดได้
---------------------------------------

< อ้างอิงรูปและ โค้ดจาก W3SCHOOL >


Week2 :What is XML


What is XML

ตัวอักษรใน แต่ ละตัวของ XML นั้นมีความหมาย คือ

X - eXtensible         
   หมายถึง คุณสมบัติ ในการเพิ่ม หรือขยาย ข้อมูล ได้ โดย ผลลัพธ์ ยังคงตรงตามที่คาดหวังเอาไว้
เช่น
<note>
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
</note>

ได้ผลการทำงานเป็น
หากเพิ่มวันเวลา เข้าไป ดังนี้
<note>
 <date>2015-09-01</date>
 <hour>08:30</hour>
 <to>Tove</to>
 <from>Jani</from>
 <body>Don't forget me this weekend!</body>
</note>

ผลลัพธ์ก็ยังได้ตามที่คาดหวังไว้เช่น

M - Markup           
เป็น รูปแบบที่มีการสร้าง mark up , tag โดยใช้ syntax ของ language เป็นตัวช่วยกำหนด rule โดย จะมีลักษณะ tag เปิด ปิด ดังลักษณะนี้
<MARKUP>Something</MARKUP>
L  - Language
เมื่อเป็น ภาษา ก็หมายถึง มีการกำหนดกฏในการเขียนล่วงหน้า เช่น
เช่น
<?xml version="1.0" encoding="UTF-8"?>
ถ้าจะกำหนด เวอร์ชั่น และ การ encode จำเป็นต้อง กำหนดที่ บรรทัดแรก เท่านั้น
<b><i>This text is bold and italic</i></b> เรียงลำดับถูกต้อง
<b><i>This text is bold and italic</b></i> เรียงลำดับผิด ไม่สามารถ ปิด tag /b ก่อน /i ได้
tag <Letter> is different from the tag <letter>.

--------------------------------------------------

  • XML is a software- and hardware-independent tool for storing and transporting data.
เก็บและส่งผ่านข้อมูลเท่านั้น ไม่เกี่ยวกับการแสดงผล   จำเป็นต้องมีการ กำหนดการแสดงผลผ่าน เครื่องมือ อื่นๆ

  • XML is Format [ Language ]
ไม่มีการกำหนด Tag มาให้ ผู้เขียนต้องกำหนดเอง แต่ก็มีข้อห้าม บางอย่าง เช่น ห้ามตั้งชื่อ Tag ว่า ‘xml’ , เป็น case sensitive ตัวเล็ก ใหญ่ มีความต่างกัน , จำเป็นต้องมี Tag ปิด และ ไม่สามารถ สลับ Tag ปิด ผิดตำแหน่งได้ เวลา ใช้ทับซ้อนกัน เป็นต้น

---------------------------------------------------------
  • XML is a W3C Recommendation

W3C  -> World Wide Web Consortium เป็นองค์กรระหว่างประเทศที่ทำงานด้านการพัฒนาเทคโนโลยี “เวิลด์ไวด์เว็บ (www)” W3C ยอมรับ XML เป็น Standard

  • XML was designed to store and transport data

  • XML was designed to be self-descriptive
เช่น จากรูปเราทราบได้ว่า ในแต่ละ Tag
เก็บข้อมูลเกี่ยวกับอะไรเอาไว้อยู่
เช่น


date = วันที่


------------------------------------------------------------

< อ้างอิงรูปและ โค้ดจาก W3SCHOOL >