none
Class 序列化 XML 問題 RRS feed

  • 问题

  • 原始的xml檔案長的像以下,我想要將利用以下的結構建立不同資訊xml檔案,所以我採用class建立欄位,然後再將class序列化成xml,以便將這些欄位反序列化綁定到BindingSource

     

    原始檔案:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

    <epcis:EPCISDocument xmlns:epcis="urn:epcglobal:epcis:xsd:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sensor="http://epcis.gs1hk.org/ns/sensor" creationDate="2010-09-15T04:07:52" schemaVersion="1.0" xmlns:gs1hk="urn:epcglobal:gs1hk:xsd:ext">

      <EPCISBody>

        <EventList>

          <ObjectEvent>

            <eventTime>2011-02-17T16:07:57</eventTime>

            <eventTimeZoneOffset>+08:00</eventTimeZoneOffset>

            <epcList>

              <epc>BBAA99887766554400085094</epc>

            </epcList>

            <action>OBSERVE</action>

            <bizStep>urn:epcglobal:cbv:bizstep:Shipping</bizStep>

            <disposition>urn:epcglobal:cbv:disposition:in_progress</disposition>

            <bizLocation>

              <id>APP Depot A</id>

            </bizLocation>

            <bizTransactionList>

              <bizTransaction type="urn:gs1hk:ext:order">order_no</bizTransaction>

            </bizTransactionList>

            <sensor:state>Running-No Alarms</sensor:state>

            <sensor:temperature>27.389999</sensor:temperature>

            <gs1hk:application>APP</gs1hk:application>

          </ObjectEvent>

        </EventList>

      </EPCISBody>

    </epcis:EPCISDocument>

     

    但是仍有些許問題,我不知從何下手,以下結構我無法xml順利建立成class

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

    <epcis:EPCISDocument xmlns:epcis="urn:epcglobal:epcis:xsd:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sensor="http://epcis.gs1hk.org/ns/sensor" creationDate="2010-09-15T04:07:52" schemaVersion="1.0" xmlns:gs1hk="urn:epcglobal:gs1hk:xsd:ext">

     

     

    以下是我實作的專案

    http://files.dotblogs.com.tw/yc421206/1105/201151012756449.zip

    .\System.ezTrack.Xml\WindowsFormsApplication1\bin\Debug\Test.xml就是我利用序列化產生的檔案,長的很像,但是仍不完全,我應該要怎麼做產生出來的檔案才能跟原始檔案相同??

    <?xml version="1.0" encoding="utf-8"?>

    <epcis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" sensor="http://epcis.gs1hk.org/ns/sensor" schemaVersion="1" creationDate="2010-09-15T04:07:52" gs1hk="urn:epcglobal:gs1hk:xsd:ext">

      <EPCISBody>

        <EventList>

          <ObjectEvent>

            <eventTime>2011-05-09T18:35:38</eventTime>

            <eventTimeZoneOffset>+08:00</eventTimeZoneOffset>

            <epcList>

              <epc>epcListl</epc>

              <epc>epcList2</epc>

              <epc>epcList3</epc>

            </epcList>

            <action>OBSERVE</action>

            <bizStep>urn:epcglobal:cbv:bizstep:Shipping</bizStep>

            <disposition>urn:epcglobal:cbv:disposition:in_progress</disposition>

            <bizLocation>

              <id>biz1</id>

              <id>ABC</id>

            </bizLocation>

            <bizTransactionList />

          </ObjectEvent>

        </EventList>

      </EPCISBody>

    </epcis>

     

    有勞各位替小的解答,小的感激不盡。

    ==========================================================

    補充一下!
    我也用過 xsd.exe來處理
    1.xsd.exe 把 *.xml 轉成 *.xsd。
    2.然後用 xsd.exe 要把 *.xsd 轉成 class(在這個步驟出錯)

    下圖是錯誤訊息畫面
    http://files.dotblogs.com.tw/yc421206/1105/2011510114453990.png

    因為轉檔錯誤才想要自己動手建立class結構


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/

    2011年5月9日 10:50

答案

  • 你的schema里面没有指定target namespace,导致生成的cs文件没有[XmlRoot(Namespace="urn:epcglobal:epcis:xsd:1")]



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月10日 16:01
    版主
  • {
           [XmlElement("bizTransactionList"
           BizTransactionList BizTransactionList{get;set;} 
    }
    public class BizTransactionList 
    {
           [XmlArrayItem("bizTransaction")]
           public BizTransaction[] BizTransactions{get;set;}
    }
     
    public class BizTransaction
    {
        [XmlAttribute()]
        public string type { get; set; }
        [XmlText()]
        public string XmlText { get; set; }
        public object _dummy;
    }


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月12日 4:46
    版主

全部回复

  • 先用XML工具生成XML对应的schema再用.Net Framework里的xsd.exe生成代码。

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月9日 18:04
    版主
  • dear sir

    XML工具指的是啥工具?有无详细的步骤?

    我有尝试使用xsd.exe将xml转成xsd,然后再将xsd转成class,我无法转成class


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年5月10日 2:15
  • 很多工具都有,自己写也可以,用XmlSchemaInference类。

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月10日 5:37
    版主
  • dear sheng jiang:

    小的使用 XmlSerializerNamespaces後便處理掉

    xmlns:epcis="urn:epcglobal:epcis:xsd:1"
    xmlns:sensor
    ="http://epcis.gs1hk.org/ns/sensor"
    xmlns:gs1hk="urn:epcglobal:gs1hk:xsd:ext"

    但以下的結構仍無法解決,我應該怎麼設定?

    epcis:EPCISDocument

     

     

    private void Form1_Load(object sender, EventArgs e)
      {
       EPCISDocument doc = new EPCISDocument();
       doc.EPCISBody.EventList.ObjectEvent.epcList.Add("epcListl");
       doc.EPCISBody.EventList.ObjectEvent.epcList.Add("epcList2");
       doc.EPCISBody.EventList.ObjectEvent.epcList.Add("epcList3");
       doc.EPCISBody.EventList.ObjectEvent.bizLocation.Add("biz1");
       doc.EPCISBody.EventList.ObjectEvent.bizLocation.Add("aaaaab");
       doc.EPCISBody.EventList.ObjectEvent.bizTransactionList.Add("ss");
    
       XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
       xmlSerializerNamespaces.Add("epcis", "urn:epcglobal:epcis:xsd:1");
       xmlSerializerNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
       xmlSerializerNamespaces.Add("sensor", "http://epcis.gs1hk.org/ns/sensor");
       xmlSerializerNamespaces.Add("gs1hk", "urn:epcglobal:gs1hk:xsd:ext");
    
       //序列化
       XmlSerializer xml = new XmlSerializer(doc.GetType());
       
       string file = xml.ToString();
       Stream stream = new FileStream("Test.xml", FileMode.Create, FileAccess.Write, FileShare.Read);
       StreamWriter writer = new StreamWriter(stream,Encoding.UTF8);
       xml.Serialize(writer, doc, xmlSerializerNamespaces);
       // xml.Serialize(writer, epcis);
       stream.Close();
       writer.Close();
      }
    

    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年5月10日 10:11
  • 你的schema里面没有指定target namespace,导致生成的cs文件没有[XmlRoot(Namespace="urn:epcglobal:epcis:xsd:1")]



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月10日 16:01
    版主
  • 你好:)小的不才,希望能够尽一臂之力

    Sheng Jiang 蒋晟MVP, 版主——所言的Xml生成器(Xml Schema)我想是不是就是VS内部自带的一个东西?

    比如你先在工程中创建一个xml文件,然后双击打开这个文件,菜单上应该会有一个Xml的选项,然后点击下拉,选择“Create Schema”即可。


    如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com;或者请QQ我
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年5月11日 1:13
  • dear Sheng Jiang:
    小的已經完成快要完成了,現在只差下列結構不知如何定義,感謝您的幫忙
        <bizTransactionList>
         <bizTransaction type="urn:gs1hk:ext:order">order_no</bizTransaction>
        </bizTransactionList>
    

    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年5月11日 7:06
  • dear Wei

    因為小的拿到的文件不是正確文件格式,所以沒有辦法用工具自行轉出


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年5月11日 7:08
  • bizTransactionList看起来是一个sequence,里面还应该有bizTransaction之类的东西的。建议联系XML作者以获取正确的XSD

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月11日 14:30
    版主
  • bizTransactionList看起来是一个sequence,里面还应该有bizTransaction之类的东西的。建议联系XML作者以获取正确的XSD

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    dear

    若不考慮XSD,該如何自定義以下區段?

    <bizTransactionList>
       <bizTransaction type="urn:gs1hk:ext:order">order_no</bizTransaction>
      </bizTransactionList>

    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年5月11日 15:53
  • Dear

    經由各位協助小的已快完成結果了,以下是我實作的結果

    http://Files.Dotblogs.com.tw/yc421206/1105/2011512105653317.zip

    但還差一個結構不知如何設定

        <bizTransactionList>

         <bizTransaction type="urn:gs1hk:ext:order">order_no</bizTransaction>

        </bizTransactionList>

     

    若不考慮來源XSD的正確性,我應該要如何定為序列化屬性?


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年5月12日 3:14
  • XSD的话,是一个只包含一个bizTransaction元素的sequence

    cs的话应该是一个bizTransaction[]的属性



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月12日 3:17
    版主
  • XSD的话,是一个只包含一个bizTransaction元素的sequence

    cs的话应该是一个bizTransaction[]的属性



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP


    以下是我的定義的class

      [XmlRoot("bizTransactionList")]
      public class BizTransactionList
      {
        private StringCollection _BizTransaction = null;
        /// <summary>
        /// 交易文件
        /// </summary>
        [XmlArrayItem("bizTransaction")]
        public StringCollection BizTransaction
        {
          get
          {
            if (this._BizTransaction == null)
              this._BizTransaction = new StringCollection();
            return this._BizTransaction;
          }
          set { this._BizTransaction = value; }
        }
      }
    

     

    引用這個class則使用以下

        private BizTransactionList _BizTransactionList = null;
        [XmlElement("bizTransactionList")]
        //[XmlElementAttribute("urn:gs1hk:ext:order")]
        public BizTransactionList BizTransactionList
        {
          get
          {
            if (this._BizTransactionList == null)
              this._BizTransactionList = new BizTransactionList();
            return this._BizTransactionList;
          }
          set { this._BizTransactionList = value; }
        }
    

     

    產生出來的結果如下

         <BizTransaction>
          <bizTransaction>ssss</bizTransaction>
         </BizTransaction>
    

    還未達到我想要的目標

      <bizTransactionList>
       <bizTransaction type="urn:gs1hk:ext:order">order_no</bizTransaction>
      </bizTransactionList>
    


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年5月12日 3:47
  • 声明一个包含名字为type,序列化为attribute的属性的bizTransaction类

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月12日 3:59
    版主
  • 声明一个包含名字为type,序列化为attribute的属性的bizTransaction类

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    dear

    小的剛試了一會但仍無法成功,可否提供部份程式碼供小的學習呢


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2011年5月12日 4:19
  • {
           [XmlElement("bizTransactionList"
           BizTransactionList BizTransactionList{get;set;} 
    }
    public class BizTransactionList 
    {
           [XmlArrayItem("bizTransaction")]
           public BizTransaction[] BizTransactions{get;set;}
    }
     
    public class BizTransaction
    {
        [XmlAttribute()]
        public string type { get; set; }
        [XmlText()]
        public string XmlText { get; set; }
        public object _dummy;
    }


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    2011年5月12日 4:46
    版主