提前谢谢你。
我知道有很多类似于我正在做的问题,我尝试了本网站和其他网站建议的大部分解决方案。但我无法将两个问题结合起来。
我有xsd文件,并使用Visual Studio的xsd.exe序列化为C#。现在我想将xml反序列化为对象。我已经忍受了差不多两天了。我无法成功反序列化文档。每行之间有很多不需要的空格,所以我使用设置来忽略这些空格。我正在使用的XML文件是UTF-8编码的。
我尝试了许多不同的方法来组合设置和使用StreamReader,但在我看来,我不能同时使用它们。
如果我只是使用设置和字符串阅读器,我会得到:
There is an error in XML document (1, 1).
如果我使用设置和streamReader,我会得到:
"Instance validation error: 'DoS: resource consumption\n\t\t\t\t\t\t(CPU)' is not a valid value for Impact_Type."}
我不太确定如何将两者结合在一起。
这是我反序列化文档的代码。
String file = @"C:\Documents and Settings\YYC\Desktop\cwec_v2.1\cwec_v2.1.xml";
//var lines = File.ReadAllText(file);
//var replace = Regex.Replace(lines, @"\s{2,}", "", RegexOptions.Multiline);
//File.WriteAllText(file, replace);
XmlReaderSettings settings = new XmlReaderSettings();
//settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
settings.CloseInput = true;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
//XmlReader reader = XmlReader.Create(new StreamReader(file), settings, context);
XmlReader reader = XmlReader.Create(new StringReader(file), settings, context);
Weakness_Catalog wC = (Weakness_Catalog)xS.Deserialize(reader);
下面我将提供XML,C#和xsd代码的一部分供您参考。
这是XML文件的一部分:
<?xml version="1.0" encoding="UTF-8"?>
<Weakness_Catalog Catalog_Name="CWE" Catalog_Version="2.1" Catalog_Date="2011-09-13"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://cwe.mitre.org/data/xsd/cwe_schema_v5.1.xsd">
<Views>
<View ID="1000" Name="Research Concepts" Status="Draft">
<View_Structure>Graph</View_Structure>
....
<View_Audience>
<Audience>
<Stakeholder>Academic_Researchers</Stakeholder>
<Stakeholder_Description>
<Text>This view provides an organizational structure for weaknesses that is
different than the approaches undertaken by taxonomies such as Seven
Pernicious Kingdoms.</Text>
</Stakeholder_Description>
这是xsd.exe生成的c#代码的一部分
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[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 Weakness_Catalog {
private View[] viewsField;
private Category[] categoriesField;
private Weakness[] weaknessesField;
private Compound_Element[] compound_ElementsField;
private string catalog_NameField;
private string catalog_VersionField;
private System.DateTime catalog_DateField;
private bool catalog_DateFieldSpecified;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("View", IsNullable=false)]
public View[] Views {
get {
return this.viewsField;
}
set {
this.viewsField = value;
}
}
......
这是XSD文件的一部分
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Weakness_Catalog">
<xs:annotation>
<xs:documentation> The Weakness_Catalog structure represents a collection of software
security issues (flaws, faults, bugs, vulnerabilities, weaknesses, etc). The name
used by CWE is usually "CWE". However, if this collection is a subset of CWE, then a
more appropriate name should be used. It has two required attributes: Catalog_Name
and Catalog_Version.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="Views">
<xs:annotation>
<xs:documentation> The Views structure contains zero or more View elements.
Each View element represents a perspective with which one might look at
the weaknesses in CWE. CWE-699 Development View and CWE-1000 Research
View are two examples of Views. </xs:documentation>
</xs:annotation>
<xs:complexType>
.....
编辑:
获得了XSD文件 获取了XML文件