如何使用XSD生成的c#类验证XML [英] How to validate XML with generated c# class from XSD

查看:116
本文介绍了如何使用XSD生成的c#类验证XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从客户端向本地服务器发送http请求,在服务器上我使用Linq查询以xml返回数据我还有.xsd文件和从我想要的xsd文件中加入的.cs文件验证我的xml。我有几个问题:



如何使用c#生成的类验证xml?

如何将xml从服务器返回到客户端?< br $> b $ b



这是我的c#课程:



I want to send a http request from client to local server and on the server I made a query with Linq that returns data in xml I also have .xsd file and a .cs file enerated from my xsd file that I want to validate my xml with. I have several questions:

how can I validate xml with c# generated class?
how can I return xml from server to client?


this is my c# class:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.34209
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.17929.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class FlashList {
    
    private FlashListFlash flashField;
    
    /// <remarks/>
    public FlashListFlash flash {
        get {
            return this.flashField;
        }
        set {
            this.flashField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class FlashListFlash {
    
    private string vinField;
    
    private string modelTypeField;
    
    public string Vin {
        get {
            return this.vinField;
        }
        set {
            this.vinField = value;
        }
    }
    public string ModelType {
        get {
            return this.modelTypeField;
        }
        set {
            this.modelTypeField = value;
        }
    }
}





这是我的XML格式:





and this is my XML format:

<FlashList>
   <flash>
      <Vin>aa123654654s6546</Vin>
      <ModelType>x23j</ModelType>
   </flash>
</FlashList>





这是我的我提出查询的主文件:





and this is my main file that I made the query:

namespace WebApplication1
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class MyWebService : System.Web.Services.WebService
    {
       
        [WebMethod]
        public XXX MyService(string vinValue)
        {
            FlashListFlash flash = new FlashListFlash();


            LinqSqlDataContext db = new LinqSqlDataContext();
            var reportdata =    from Vin in db.Vin
                                from Global in db.Globals
                                from Associate in db.Associas
                                where Vin.Vin == vinValue && Vin.NHard == Global.NHard &&
                                Global.NomeFile == Associate.KeyJoined &&
                                SqlMethods.Like(Global.NVerHard, "0" + Vin.NVerHard)
                                select new XElement("flash",
                                    new XElement("Vin", Vin.Vin),
                                    new XElement("ModelType", Associate.Model_Type)
                                );



            XElement reportXml = new XElement("FlashList", reportdata);
            string fileName = @"D:\FlashList.xml";
            Directory.CreateDirectory(Path.GetDirectoryName(fileName));
            reportXml.Save(fileName);
            return XXX;
        }
    }
}

推荐答案

Wikikipedia写道:
Wikkipedia wrote:

XML验证是检查用XML(可扩展标记语言)编写的文档的过程,以确认它既是格式良好的,也是有效的,因为它遵循定义的结构。一个结构良好的文档遵循XML的基本语法规则,这些规则对于所有XML文档都是相同的。[1]有效文档还遵守特定DTD或XML模式规定的规则,根据特定应用程序的特定选择

XML validation is the process of checking a document written in XML (eXtensible Markup Language) to confirm that it is both well-formed and also "valid" in that it follows a defined structure. A well-formed document follows the basic syntactic rules of XML, which are the same for all XML documents.[1] A valid document also respects the rules dictated by a particular DTD or XML schema, according to the application-specific choices for those particular



来源: http://en.wikipedia.org/wiki/XML_validation [ ^ ]



我建议读这个:

如何:使用XSD验证(LINQ to XML) [ ^ ]

使用XmlSchemaSet进行XML架构(XSD)验证 [ ^ ]

XmlSchemaValidator类 [ ^ ]


Source: http://en.wikipedia.org/wiki/XML_validation[^]

I'd suggest to read this:
How to: Validate Using XSD (LINQ to XML)[^]
XML Schema (XSD) Validation with XmlSchemaSet[^]
XmlSchemaValidator Class[^]


这篇关于如何使用XSD生成的c#类验证XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆