【发布时间】:2014-07-01 05:54:52
【问题描述】:
我是一个新手,正在学习使用 XML 编程......我创建了一个上传 xml 文件的功能......上传 xml(这是带有用户数据的简单问答数据)和验证功能抛出异常...
e.Message = "'id' 元素无效 - 值 '43516' 是 根据其数据类型无效 'http://www.w3.org/2001/XMLSchema:short' - 字符串 '43516' 不是 有效的 Int16 值。”
这是我的功能
[WebMethod]
public static bool CheckFile(string filename)
{
surgeProtection = true;
bool returnval = false;
String xsdPath = "";
//Read the path to upload on the web server
string Uploadpath = ConfigurationManager.AppSettings["UploadPath"];
xsdPath = Uploadpath + "\\" + "survey.xsd";
////Validate the uploaded files on the web server
XmlSchemaSet schemas = new XmlSchemaSet(); //intialize schema class
using (FileStream schemastream = File.OpenRead(xsdPath)) //xsd file load
{
schemas.Add(XmlSchema.Read(schemastream, new ValidationEventHandler(OnValidate)));
//create event for schema
}
schemas.Compile();
String xmlPath = filename;
xmlPath = Uploadpath + "\\" + filename;
XmlDocument doc = new XmlDocument();
byte[] mybyte = Dashboard.Model.Surveys.SurveyService.GetImageFromDB(filename);
string xml = Encoding.UTF8.GetString(mybyte);
doc.LoadXml(xml);
doc.Schemas = schemas; // take schema
doc.Validate(OnValidate); // validate schema
returnval = surgeProtection;
return returnval;
}
This is my validation Function
public static void OnValidate(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
surgeProtection = false;
sw.WriteLine("Error: " + e.Message);
}
}
this is my xml
<?xml version="1.0" encoding="utf-8"?>
<survey>
<title>xxxxxxxxxx</title>
<questions>
<question>
<description>TestDescription</description>
<type>grid</type>
<id>43516</id>
<options>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</options>
<sub-question description="-" id="43516_01">
<response>
<answer user="xxxx@xxx.com">7</answer>
</response>
</sub-question>
</question>
..and so on questions
</questions>
<users>
<user iRecipientId="" sEmail="xxxx" city="xx" country="xx" responseDate="xxx"/>
..and so on
</users>
</survey>
这是我在 DB 中的表格设计
ID int Unchecked
ImageName varchar(200) Unchecked
Image varbinary(MAX) Unchecked
XML 很大...但是我刚刚展示了基本格式...我已经了解 ptoblem 与列 43516 abd suqestion ...
然后我需要分配什么数据类型,我需要在哪里更改数据类型??我需要在数据库表中更改吗?在 SQL 数据类型中是 int 或 bigint ...不是 short ..long ..我需要更改为 bigint 吗? 任何建议都会有帮助
【问题讨论】:
-
此错误已解决...架构中有元素
...显示错误...我更改更改那个“长”的错误解决了......但是另一个错误来了......“没有声明'id'属性。” ...我需要在哪里更改“id”?