我想验证或查找XSD文件中是否存在元素。
我的XSD架构如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!-- XSD -->
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://integration.cbre.com/schemas/gws/processinvoice/v1" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="Type_DATE">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Type_Document1">
<xs:sequence>
<xs:element name="ID" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Name" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Address" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="City" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="State" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Comments" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Type_Document2">
<xs:sequence>
<xs:element name="Sender" type="Type_Document1" minOccurs="0" maxOccurs="1"/>
<xs:element name="Receiver" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Email" type="xs:string" maxOccurs="1"/>
<xs:element name="Type" type="xs:string" maxOccurs="1"/>
<xs:element name="Remarks" type="Type_Document3" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Type_Document3">
<xs:sequence>
<xs:element name="Phone" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Fax" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Email" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Address" type="xs:string" minOccurs="0" maxOccurs="1"/>
<xs:element name="Comments" type="xs:string" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
现在在c#或vb.net中,我需要验证XSD架构中是否存在特定字段。
e.g。如果我将以下参数传递给函数,它应该返回true
//如果我们传递了Document1
,应该使用like与Type_Document1匹配Function ValidateColumn(Type as string, Element as string) as boolean
End Function
但如果传递跟随,那么它应该返回false,因为这些元素不存在。
答案 0 :(得分:0)
这里有一些可以帮助你的VB.NET代码:
Dim dataSet As Data.DataSet = New Data.DataSet
'add xsd with tables and columns into data table
dataSet.ReadXmlSchema(params.XsdPath)
'loop through each table and col
For Each dtable As Data.DataTable In DataSet.Tables
For Each col As Data.DataColumn In dtable.Columns
'could do a case statement here on document names which would be
'your table names
'then do if col equals your element, true
Next
Next
答案 1 :(得分:0)
我使用C# programming language
和System.Xml.Linq
库进行qustion,如下所示:
我这样写了ValidateColumn
metod:
bool ValidateColumn(string rootTypeName, string fieldName)
{
bool contains = false;
var xDoc = XDocument.Load(yourXsdFilePath);
foreach (var typeElement in xDoc.Root.Elements())
{
foreach (var field in typeElement.Elements().FirstOrDefault().Elements())
{
// checks if parent type element name equals give name, and child field element name equals given fieldName
// and check this validation if your rootTypeName begins with 'Type_'
int indexOfTypePrefix = typeElement.FirstAttribute.Value.IndexOf("Type_");
if ((typeElement.FirstAttribute.Value == rootTypeName && field.FirstAttribute.Value == fieldName)
||
(indexOfTypePrefix == 0 && typeElement.FirstAttribute.Value.Substring(indexOfTypePrefix + 5) == rootTypeName
&& field.FirstAttribute.Value == fieldName)
)
{
contains = true;
break;
}
}
if (contains)
break;
}
return contains;
}
我在console application
测试了它,它给出了你想要的答案。
Console.WriteLine("Function Validatetion for {0}, {1}: {2}", "Type_Document1", "State", ValidateColumn("Type_Document1", "State"));
// prints: Function Validatetion for Type_Document1, State: True
Console.WriteLine("Function Validatetion for {0}, {1}: {2}", "Document1", "State", ValidateColumn("Document1", "State"));
// prints: Function Validatetion for Document1, State: True
Console.WriteLine();
Console.WriteLine("Function Validatetion for {0}, {1}: {2}", "Type_Document2", "Designation", ValidateColumn("Type_Document2", "Designation"));
// prints: Function Validatetion for Type_Document2, Designation: False
Console.WriteLine("Function Validatetion for {0}, {1}: {2}", "Document2", "Title", ValidateColumn("Document2", "Title"));
// prints: Function Validatetion for Document2, Title: False
PS。我使用FirstAttribute
属性获取属性名称,因此必须首先在节点中为此代码编写name属性。您可以优化此代码。