2012-06-15 53 views
0

下面是從一個XSD和XML的示例C#的XmlDocument不與相應的XSD默認更新屬性

XML

<?xml version="1.0" encoding="UTF-8"?> 
<config test2="true"> 
</config> 

XSD

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema targetNamespace="rules.xsd" xmlns="rules.xsd" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"> 
    <xs:element name="config"> 
    <xs:complexType> 
     <xs:attribute name="test1" type="xs:boolean" default="false" /> 
     <xs:attribute name="test2" type="xs:string" default="mary123" /> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

我可以使用此XSD到使用這段代碼在C#中驗證此xml

XmlDocument doc = new XmlDocument(); 
XmlTextReader schemaReader = new XmlTextReader(System.IO.Path.GetFullPath("Mi_XSD_here.xsd")); 
XmlSchema schema = XmlSchema.Read(schemaReader, ValidationCallBack); 
doc.Schemas.Add(schema); 
doc.Load("Mi_XML_here.xml"); 
doc.Validate(ValidationCallBack); 

問題是:我在xsd中有兩個默認屬性,但是當我運行此代碼時,他沒有在XmlDocument中插入屬性,結果與我傳遞給系統的xml相同。

的默認屬性不工作,我不能弄清楚他們爲什麼不工作,我相信存在其他形式來解決這個問題,我沒有發現這一點,但沒有工作

Extensions.Validate Method

obs:ValidationCallBack是我認爲與問題無關的一些錯誤返回函數

回答

0

您的模式的目標命名空間是rules.xsd,因此您的xml文件需要包含根據模式進行驗證的文件。當然我也會假設teste2是一個錯字(因爲不符合模式),你的意思測試2:

<config test2="" xmlns="rules.xsd" /> 

在這種情況下,只有test1的將被添加一個默認的,因爲test2的已被設置爲一個空字符串。

+0

非常感謝你的配合。這是如此簡單,我失去了這麼多的時間... ... – Higarian