我使用了一些xsdMarkup
string xsdMarkup =
@"<?xml version='1.0' encoding='utf-8'?>
<xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='catalog'>
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs='unbounded' name='book'>
<xs:complexType>
<xs:sequence>
<xs:element name='authors'>
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs='unbounded' name='author' type='xs:string' />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='title' type='xs:string' maxOccurs='1' minOccurs='1' />
<xs:element name='genre' type='xs:string' maxOccurs='1' minOccurs='1' />
<xs:element name='price' type='xs:string' maxOccurs='1' minOccurs='1'/>
<xs:element name='publish_date' type='xs:string' maxOccurs='1' />
<xs:element name='description' type='xs:string' maxOccurs='1' />
</xs:sequence>
<xs:attribute name='id' type='xs:unsignedByte' use='required' />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
下一个方法验证:
public string ValidateXml(XDocument doc)
{
string errors_text = "";
bool errors = false;
doc.Validate(schemas, (o, e) =>
{
MessageBox.Show(e.Message);
errors = true;
errors_text += e.Message;
});
Label1.Content = (errors == true) ? "nie przeszło" : "przeszło";
return errors_text;
}
打开文件并验证该文件:
private void OpenFile_Click(object sender, RoutedEventArgs e)
{
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
openFileDialog.Filter = "XML files (*.XML)|*.XML|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == true)
{
XDocument asd = XDocument.Load(openFileDialog.FileName);
ValidateXml(asd);
}
但是如果我的文件错过了标题,流派,发布_日期,价格我没有得到任何错误,我想知道为什么会这样?你能帮我解决这个问题吗?
这是我加载的示例.xml文件
<?xml version="1.0"?>
<catalog>
<book id="001">
<authors>
<author>P</author>
<author>A</author>
</authors>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="002">
<authors>
<author>K</author>
</authors>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
<book id="003">
<authors>
<author></author>
</authors>
<title></title>
<genre></genre>
<price></price>
<publish_date></publish_date>
<description></description>
</book>
<book id="004">
<authors>
<author>asd</author>
</authors>
<title>asd</title>
<genre>asdasd</genre>
<price></price>
<publish_date></publish_date>
<description></description>
</book>
</catalog>
答案 0 :(得分:0)
我注意到您的XML文件在每种情况下都有FragmentTransaction transaction = this.getActivity().getSupportFragmentManager().beginTransaction();
,title
和genre
的标记。只是其中一些是空的。
所有这些都是字符串,空字符串是有效的字符串。所以(我假设)XML解析器认为这些标签具有有效值。
所有架构指定的是标签需要在那里。它没有说明它们包含的字符串需要看起来像什么。
如果要将空标记视为错误,则可能必须优化模式以包含对值的进一步限制(例如最小长度或价格的非字符串数据类型)。