2013-04-05 159 views
0

我遇到了XML問題,並在XML文件中寫入了一些信息。
我有幾個描述我的XML的xsd文件。我用"xsd/c testfile1.xsd testFile2.xsd..."等創建了一個大的.cs文件,而且一切都很好,看起來不錯。
但是,如果我帶一個創建的類,即testfile1.xsd,它看起來像 "<xs:complexType name="Header">"並在裏面有一個普通的xs:元素和東西,但也是這樣的:"<xs:attribute name="version" default="1.0.0.0"/>"。這是翻譯爲:將屬性值獲取到XML文件

"public Header() { 
this.versionField = "1.0.0.0";}" 

在生成的類'標題'。它也得到了這個領域:private string versionField;

。 (當然還有其他一些私人領域,但那些作品很好。)。所以,我創建的所有類的實例,有數據填充它們,並把它寫成一個XML文件與此:

- XmlSerializer XmlSerRoot = new XmlSerializer(typeof(RootInformation)) 
    (the root of my xml!) 
- StringWriterWithEncoding strWriter = new StringWriterWithEncoding(Encoding.GetEncoding("iso-8859-1")); 
- XmlDocument documentInXML = new XmlDocument(); 
- XmlSerRoot.Serialize(strWriter, rootInformation); (Here is the XML, filled with values, and the Header.version got the value 1.0.0.0) 
- string XmlString; 
- XmlString = strWriter.ToString(); (Here I can look at the created xml when debugging in VS and now the version-information is gone) 
- documentInXML.LoadXml(XmlString); 
- documentInXML.Save(myPath); 


但是,當我看的xml文件這<Header>是沒有什麼喜歡的版本。我想我的樣子:<Header version="1.0.0.0">
我甚至試圖做的代碼,如:

Header header = new Header(); 
header.version = header.version; 
and 
with header.version = "1.0.0.0"; 

但仍然是在標籤沒有版本的文字。所有其他標籤都有它們的價值。只是這<Header>鬆散這額外的信息。
有人得到小費嗎?有很多地方我需要這個工作。其他一切工作正常。

問候,/ E

這裏是一段例子代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Serialization; 
using System.IO; 
using System.Xml; 
namespace TestAppXML 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      RootInfo rootInfo = new RootInfo(); 
      rootInfo.RootText = "This is the Root!"; 
      Header header = new Header(); 
      header.TestHeader = "This is HeaderText!"; 
      rootInfo.Header = header; 
      XmlSerializer XmlSerRoot = new XmlSerializer(typeof(RootInfo)); 
      StringWriterWithEncoding strWriter = new StringWriterWithEncoding(Encoding.GetEncoding("iso-8859-1")); 
      XmlDocument documentInXML = new XmlDocument(); 
      XmlSerRoot.Serialize(strWriter, rootInfo); 
      string XmlString; 
      XmlString = strWriter.ToString(); 
      documentInXML.LoadXml(XmlString); 
      strWriter.Close(); 
      documentInXML.Save(@"C:\TestXml.xml"); 
     } 
    } 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://acme.com")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://acme.com", IsNullable = false)] 
    public partial class RootInfo 
    { 
     private Header headerField; 
     private string rootTextField; 
     public Header Header 
     { 
      get { return this.headerField; } 
      set { this.headerField = value; } 
     } 

     [System.Xml.Serialization.XmlElementAttribute(DataType = "normalizedString")] 
     public string RootText 
     { 
      get { return this.rootTextField; } 
      set { this.rootTextField = value; } 
     } 
    } 

     [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
     [System.SerializableAttribute()] 
     [System.Diagnostics.DebuggerStepThroughAttribute()] 
     [System.ComponentModel.DesignerCategoryAttribute("code")] 
     [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://acme.com")] 
     public partial class Header 
     { 
      private string testHeaderField; 
      private string versionField; 
      public Header() 
      { 
       this.versionField = "1.0.0.0"; 
      } 

      /// <remarks/> 
      [System.Xml.Serialization.XmlElementAttribute(DataType = "normalizedString")] 
      public string TestHeader 
      { 
       get { return this.testHeaderField; } 
       set { this.testHeaderField = value; } 
      } 

      [System.Xml.Serialization.XmlAttributeAttribute()] 
      [System.ComponentModel.DefaultValueAttribute("1.0.0.0")] 
      public string version 
      { 
       get { return this.versionField; } 
       set { this.versionField = value; } 
      } 
     } 
     class StringWriterWithEncoding : StringWriter 
     { 
      private Encoding MyEncoding; 
      public StringWriterWithEncoding(Encoding encoding) 
       : base() 
      {MyEncoding = encoding;} 
      public override Encoding Encoding 
      { 
       get{return MyEncoding;} 
      } 
     } 
    } 
+0

你可以粘貼你正在使用的實際代碼。 – alykhalid 2013-04-05 08:17:02

+0

好吧,我放入一個小例子,其中這個版本文本不在xml文件中。 問候,/電子 – 2013-04-05 11:25:26

回答

0

沒關係,我想我knov問題。這是因爲這個xsd.exe爲這些字段創建了'DefaultValueAttribute'。這阻止了這個字段在xml中。也許一些xsd-switch可以做到這一點,我不知道...