如何使用XSD文件验证输入c#对象? [英] How to validate input c# object using XSD file?
问题描述
您好,
我想使用我定义的xsd文件验证我的输入对象。你能告诉我怎么做吗?
以下是我的c#对象
Hello,
I would like to validate my input object using the xsd file I defined. Would you please let me know how to do this?
below is my c# object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BaseModel
{
public class Test
{
public string TestID { get; set; }
public string SocialSecurityNo { get; set; }
public string OrgSelection { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}
}
下面是序列化输入对象后得到的XML
below is the XML I get after serializing my input object
<?xml version="1.0" encoding="utf-8" ?>
<Personnel>
<EnterpriseID>1</EnterpriseID>
<SocialSecurityNo>12345678</SocialSecurityNo>
<OrgSelection/>
<MiddleName/>
<LastName>Tharigopula</LastName>
</Personnel>
以下是XSD文件我使用输入对象生成
Below is the XSD file I generated using the input object
<xs:schema elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Personnel" nillable="true" type="Personnel" />
<xs:complextype name="Test">
<xs:sequence>
<xs:element minoccurs="1" maxoccurs="1" name="TestID" type="xs:string" />
<xs:element minoccurs="1" maxoccurs="1" name="SocialSecurityNo" type="xs:string" />
<xs:element minoccurs="0" maxoccurs="1" name="OrgSelection" type="xs:string" />
<xs:element minoccurs="1" maxoccurs="1" name="FirstName" type="xs:string" />
<xs:element minoccurs="0" maxoccurs="1" name="MiddleName" type="xs:string" />
<xs:element minoccurs="1" maxoccurs="1" name="LastName" type="xs:string" />
</xs:sequence>
</xs:complextype>
</xs:schema>
正如您在XSD中看到的,我有TestId,SocialSecurityNo ,FirstName和LastName或必填字段以及OrgSelection和MiddleName或可选字段..
所以如果我在不传递FirstName和LastName的情况下验证我的输入对象,我应该看到验证错误..
有人可以帮忙吗?
As you can see in the XSD, I have the TestId, SocialSecurityNo, FirstName, and LastName or the mandatory fields and OrgSelection and MiddleName or optional fields..
so if I were validate my input object without passing the FirstName and LastName, I should see the validation errors..
Can someone please help?
推荐答案
嗨Ravindranath.net,
您可以使用验证
扩展方法对XSD验证XML。
以下是显示如何操作的代码。
Hi Ravindranath.net,
You can validate a XML against a XSD using theValidate
extension method.
Here is the code showing how to do it.
XmlSchemaSet schemaSet = new XmlSchemaSet();
DirectoryInfo dir = new DirectoryInfo("<path where="" you="" have="" your="" xsd="" files="">");
foreach (FileInfo fileInfo in dir.GetFiles("*.xsd"))
{
schemaSet.Add(null, fileInfo.FullName);
}
XDocument docToValidate = XDocument.Load("<path of="" xml="" you="" want="" to="" validate="">");
string validationMessage = string.Empty;
docToValidate.Validate(schemaSet, (o, e) =>
{
validationMessage += e.Message + Environment.NewLine;
});
return validationMessage;</path></path>
此代码可能需要一些自定义满足您的要求。希望这会有所帮助。
This code may need some customization to meet your requirements. Hope this helps.
这篇关于如何使用XSD文件验证输入c#对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!