1

是否可以获取存储在 HttpPostedFileBase 属性中的 xml 文件(来自 MVC 表单),并创建一个 XMLReader.Create(..,..) 对象,维护行号?

下面的示例使用硬编码的 xml 文件位置。

理想情况下 XmlReader.Create(MyHttpPostedFileBase, rs);

 public static bool Validate()
    {
        try
        {          
            string XsdPath = @"C:\Projects\Mvc\Xsd\books.xsd";
            string XmlPath = @"C:\Projects\Mvc\Xsd\food.xml"; //replace with my HttpPostedFileBase 

            XmlSchemaSet set = new XmlSchemaSet();
            set.Add(null, XsdPath);

            XmlReaderSettings rs = new XmlReaderSettings();
            rs.Schemas = set;
            rs.ValidationType = ValidationType.Schema;
            rs.ValidationEventHandler += new ValidationEventHandler(rs_ValidationEventHandler);

            using (XmlReader reader = XmlReader.Create(XmlPath, rs))
            {
                while (reader.Read()) ;
            }
        }

static void rs_ValidationEventHandler(object sender, ValidationEventArgs e)
    {                    
       //code
    }
4

1 回答 1

1

设法找出解决方案-希望这对其他人有所帮助。

从 HttpPostedFileBase Xml 上传创建 XmlDocument 的问题是 XmlDocument 组件不维护行号。

解决方案是从 HttpPostedFileBase 创建一个流并将其插入 XmlReader.Create()。

下面的代码做了两件事:验证 XML 是否有效,然后验证 XML 是否满足 XSD。

注意:我还没有时间重构代码,但它应该是可以理解的

public HttpPostedFileBase MyFile { get; set; }

public Stream GetXmlStream()
    {
        using (Stream inputStream = MyFile.InputStream)
        {
            MemoryStream memoryStream = inputStream as MemoryStream;
            memoryStream = new MemoryStream();
            inputStream.CopyTo(memoryStream);
            memoryStream.Position = 0;
            return memoryStream;
        }
    }

public class XmlValidator
{
    public string Error;

    public bool ValidateXml(Stream memoryStream)
    {
        try
        {
            string XsdPath = @"C:\Projects\Mvc\Xsd\books.xsd";
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add("", XsdPath);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(memoryStream, settings);
            XmlDocument document = new XmlDocument();

            try
            {
                document.Load(reader);
            }
            catch (XmlException ex)
            {
                UploadedFile.Error = String.Format("Line {0}, position {1}: {2}", ex.LineNumber, ex.LinePosition,
                    ex.Message);
                return false;
            }

            ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
            document.Validate(eventHandler);
            reader.Close();
            return true;
        }
        catch (XmlSchemaValidationException ex)
        {
           UploadedFile.Error = String.Format("Line {0}, position {1}: {2}", ex.LineNumber, ex.LinePosition, ex.Message);
        }
        return false;

    }

    private void ValidationEventHandler(object sender, ValidationEventArgs e)
    {
    }




}
于 2015-02-19T11:12:24.243 回答