当前位置: 首页 > 知识库问答 >
问题:

使用自动生成的架构取消XML文件的实值时,“无法识别指定的类型”

鲜于海
2023-03-14

我想解释到目前为止我的整个过程,因为我是使用XML文件的新手,希望有人能指出我在哪一步搞砸了。如果你只想看看我的问题,我会在底部简明扼要地问它。

<PAW_Items xmlns:paw="urn:schemas-peachtree-com/paw8.02-datatypes"
        xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2000/10/XMLSchema-datatypes">
    <PAW_Item xsi:type="paw:item">
        <ID xsi:type="paw:id">00002</ID>
        <Description>Example1</Description>
        <Class>1</Class>
        <isInactive>TRUE</isInactive>
        <Subject_to_Commission>FALSE</Subject_to_Commission>
        <Description_for_Sales>Example1</Description_for_Sales>
        <Sales_Prices>
            <Sales_Price_Info Key="1">
                <Sales_Price>5.22000</Sales_Price>
                <Sales_Price_Calc>NC</Sales_Price_Calc>
                <Sales_Price_Rounding>0</Sales_Price_Rounding>
                <Sales_Price_Rounding_Cent>0.00000</Sales_Price_Rounding_Cent>
            </Sales_Price_Info>
            <Sales_Price_Info Key="2">
                <Sales_Price>0.00000</Sales_Price>
                <Sales_Price_Calc>NC</Sales_Price_Calc>
                <Sales_Price_Rounding>0</Sales_Price_Rounding>
                <Sales_Price_Rounding_Cent>0.00000</Sales_Price_Rounding_Cent>
            </Sales_Price_Info>
        </Sales_Prices>
        <Tax_Type>0</Tax_Type>
        <Last_Unit_Cost>5.22000</Last_Unit_Cost>
        <Costing_Method>1</Costing_Method>
        <GL_Sales_Account xsi:type="paw:id">40000</GL_Sales_Account>
        <GL_Inventory_Account xsi:type="paw:id">12000</GL_Inventory_Account>
        <GL_COGSSalary_Acct xsi:type="paw:id">50000</GL_COGSSalary_Acct>
        <Type>SA</Type>
        <Stocking_UM>BTL</Stocking_UM>
        <Weight>0.00000</Weight>
        <Minimum_Stock>0.00000</Minimum_Stock>
        <Reorder_Quantity>0.00000</Reorder_Quantity>
        <Vendor_ID xsi:type="paw:id">B&amp;L</Vendor_ID>
        <Substock_Primary_Attributes>
        </Substock_Primary_Attributes>
        <Substock_Secondary_Attributes>
        </Substock_Secondary_Attributes>
        <QuantityOnSO>0.00000</QuantityOnSO>
        <QuantityOnPO>0.00000</QuantityOnPO>
        <QuantityOnHand>0.00000</QuantityOnHand>
        <IsTaxable>TRUE</IsTaxable>
        <Print_Components>FALSE</Print_Components>
        <Number_of_Components>0</Number_of_Components>
        <Components>
            <Component>
                <Effective_Date xsi:type="paw:date"/>
                <Component_Number>0</Component_Number>
                <Qty_Needed>0.00000</Qty_Needed>
            </Component>
        </Components>
    </PAW_Item>
    <PAW_Item xsi:type="paw:item">
        <ID xsi:type="paw:id">0001</ID>
        <Description>Example2</Description>
        <Class>1</Class>
        <isInactive>TRUE</isInactive>
        <Subject_to_Commission>FALSE</Subject_to_Commission>
        <Description_for_Sales>Example2</Description_for_Sales>
        <Sales_Prices>
            <Sales_Price_Info Key="1">
                <Sales_Price>4.39000</Sales_Price>
                <Sales_Price_Calc>NC</Sales_Price_Calc>
                <Sales_Price_Rounding>0</Sales_Price_Rounding>
                <Sales_Price_Rounding_Cent>0.00000</Sales_Price_Rounding_Cent>
            </Sales_Price_Info>
            <Sales_Price_Info Key="2">
                <Sales_Price>0.00000</Sales_Price>
                <Sales_Price_Calc>NC</Sales_Price_Calc>
                <Sales_Price_Rounding>0</Sales_Price_Rounding>
                <Sales_Price_Rounding_Cent>0.00000</Sales_Price_Rounding_Cent>
            </Sales_Price_Info>
        </Sales_Prices>
        <Tax_Type>0</Tax_Type>
        <Last_Unit_Cost>4.39000</Last_Unit_Cost>
        <Costing_Method>1</Costing_Method>
        <GL_Sales_Account xsi:type="paw:id">40000</GL_Sales_Account>
        <GL_Inventory_Account xsi:type="paw:id">12000</GL_Inventory_Account>
        <GL_COGSSalary_Acct xsi:type="paw:id">50000</GL_COGSSalary_Acct>
        <Type>SA</Type>
        <Stocking_UM>EA</Stocking_UM>
        <Weight>0.00000</Weight>
        <Minimum_Stock>0.00000</Minimum_Stock>
        <Reorder_Quantity>0.00000</Reorder_Quantity>
        <Substock_Primary_Attributes>
        </Substock_Primary_Attributes>
        <Substock_Secondary_Attributes>
        </Substock_Secondary_Attributes>
        <QuantityOnSO>0.00000</QuantityOnSO>
        <QuantityOnPO>0.00000</QuantityOnPO>
        <QuantityOnHand>0.00000</QuantityOnHand>
        <IsTaxable>TRUE</IsTaxable>
        <Print_Components>FALSE</Print_Components>
        <Number_of_Components>0</Number_of_Components>
        <Components>
            <Component>
                <Effective_Date xsi:type="paw:date"/>
                <Component_Number>0</Component_Number>
                <Qty_Needed>0.00000</Qty_Needed>
            </Component>
        </Components>
    </PAW_Item>
</PAW_Items>
xsd ITEM.xml

item.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="PAW_Items" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:app1="http://www.w3.org/2000/10/XMLSchema-instance">
  <xs:import namespace="http://www.w3.org/2000/10/XMLSchema-instance" schemaLocation="ITEM_app1.xsd" />
  <xs:element name="PAW_Items" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="PAW_Item">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Description" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
              <xs:element name="Class" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
              <xs:element name="isInactive" type="xs:string" minOccurs="0" msdata:Ordinal="3" />
              <xs:element name="Subject_to_Commission" type="xs:string" minOccurs="0" msdata:Ordinal="4" />
              <xs:element name="Description_for_Sales" type="xs:string" minOccurs="0" msdata:Ordinal="5" />
              <xs:element name="Tax_Type" type="xs:string" minOccurs="0" msdata:Ordinal="6" />
              <xs:element name="Last_Unit_Cost" type="xs:string" minOccurs="0" msdata:Ordinal="7" />
              <xs:element name="Costing_Method" type="xs:string" minOccurs="0" msdata:Ordinal="8" />
              <xs:element name="Type" type="xs:string" minOccurs="0" msdata:Ordinal="9" />
              <xs:element name="Stocking_UM" type="xs:string" minOccurs="0" msdata:Ordinal="10" />
              <xs:element name="Weight" type="xs:string" minOccurs="0" msdata:Ordinal="11" />
              <xs:element name="Minimum_Stock" type="xs:string" minOccurs="0" msdata:Ordinal="12" />
              <xs:element name="Reorder_Quantity" type="xs:string" minOccurs="0" msdata:Ordinal="13" />
              <xs:element name="Substock_Primary_Attributes" type="xs:string" minOccurs="0" msdata:Ordinal="14" />
              <xs:element name="Substock_Secondary_Attributes" type="xs:string" minOccurs="0" msdata:Ordinal="15" />
              <xs:element name="QuantityOnSO" type="xs:string" minOccurs="0" msdata:Ordinal="16" />
              <xs:element name="QuantityOnPO" type="xs:string" minOccurs="0" msdata:Ordinal="17" />
              <xs:element name="QuantityOnHand" type="xs:string" minOccurs="0" msdata:Ordinal="18" />
              <xs:element name="IsTaxable" type="xs:string" minOccurs="0" msdata:Ordinal="19" />
              <xs:element name="Print_Components" type="xs:string" minOccurs="0" msdata:Ordinal="20" />
              <xs:element name="Number_of_Components" type="xs:string" minOccurs="0" msdata:Ordinal="21" />
              <xs:element name="ID" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent msdata:ColumnName="ID_Text" msdata:Ordinal="1">
                    <xs:extension base="xs:string">
                      <xs:attribute ref="app1:type" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="Sales_Prices" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Sales_Price_Info" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Sales_Price" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
                          <xs:element name="Sales_Price_Calc" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
                          <xs:element name="Sales_Price_Rounding" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
                          <xs:element name="Sales_Price_Rounding_Cent" type="xs:string" minOccurs="0" msdata:Ordinal="3" />
                        </xs:sequence>
                        <xs:attribute name="Key" type="xs:string" />
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="GL_Sales_Account" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent msdata:ColumnName="GL_Sales_Account_Text" msdata:Ordinal="1">
                    <xs:extension base="xs:string">
                      <xs:attribute ref="app1:type" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="GL_Inventory_Account" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent msdata:ColumnName="GL_Inventory_Account_Text" msdata:Ordinal="1">
                    <xs:extension base="xs:string">
                      <xs:attribute ref="app1:type" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="GL_COGSSalary_Acct" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent msdata:ColumnName="GL_COGSSalary_Acct_Text" msdata:Ordinal="1">
                    <xs:extension base="xs:string">
                      <xs:attribute ref="app1:type" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="Vendor_ID" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent msdata:ColumnName="Vendor_ID_Text" msdata:Ordinal="1">
                    <xs:extension base="xs:string">
                      <xs:attribute ref="app1:type" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element name="Components" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Component" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="Component_Number" type="xs:string" minOccurs="0" />
                          <xs:element name="Qty_Needed" type="xs:string" minOccurs="0" />
                          <xs:element name="Effective_Date" minOccurs="0" maxOccurs="unbounded">
                            <xs:complexType>
                              <xs:attribute ref="app1:type" />
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute ref="app1:type" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

item_app1.xsd:

<?xml version="1.0" standalone="yes"?>
<xs:schema targetNamespace="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:mstns="http://www.w3.org/2000/10/XMLSchema-instance" xmlns="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:app1="http://www.w3.org/2000/10/XMLSchema-instance">
  <xs:attribute name="type" msdata:Prefix="xsi" type="xs:string" />
</xs:schema>

然后,我尝试只使用命令xsd item.xsd/c,但这不起作用。

E:\test>xsd ITEM.xsd /c
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.17929]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: The 'http://www.w3.org/2000/10/XMLSchema-instance:type' attribute is not declared. Line 113, position 14.
Schema validation warning: The 'http://www.w3.org/2000/10/XMLSchema-instance:type' attribute is not declared. Line 35, position 24.
Schema validation warning: The 'http://www.w3.org/2000/10/XMLSchema-instance:type' attribute is not declared. Line 61, position 24.
Schema validation warning: The 'http://www.w3.org/2000/10/XMLSchema-instance:type' attribute is not declared. Line 70, position 24.
Schema validation warning: The 'http://www.w3.org/2000/10/XMLSchema-instance:type' attribute is not declared. Line 79, position 24.
Schema validation warning: The 'http://www.w3.org/2000/10/XMLSchema-instance:type' attribute is not declared. Line 88, position 24.
Schema validation warning: The 'http://www.w3.org/2000/10/XMLSchema-instance:type' attribute is not declared. Line 103, position 32.

Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.

Error: Error generating classes for schema 'ITEM'.
  - The attribute type is missing.

If you would like more help, please type "xsd /?".
<xsd xmlns='http://microsoft.com/dotnet/tools/xsd/'>
<generateClasses language='CS' namespace='Peachtree'>
    <schema>ITEM.xsd</schema>
    <schema>ITEM_app1.xsd</schema>
</generateClasses>
</xsd>
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;
using Peachtree;

namespace Sandbox_Console
{
    public class Program
    {
        private static void Main(string[] args)
        {
            using (var file = File.OpenText(@"E:\test\ITEM.xml"))
            {

                var ser = new XmlSerializer(typeof(PAW_Items)); //Line 16
                var result = (PAW_Items) ser.Deserialize(file);

                Debugger.Break();
            }
        }
    }
}
System.InvalidOperationException occurred
  HResult=-2146233079
  Message=Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'Peachtree.PAW_ItemsPAW_ItemSales_PricesSales_Price_Info[]' to 'Peachtree.PAW_ItemsPAW_ItemSales_PricesSales_Price_Info'
error CS0030: Cannot convert type 'Peachtree.PAW_ItemsPAW_ItemComponentsComponent[]' to 'Peachtree.PAW_ItemsPAW_ItemComponentsComponent'
error CS0029: Cannot implicitly convert type 'Peachtree.PAW_ItemsPAW_ItemSales_PricesSales_Price_Info' to 'Peachtree.PAW_ItemsPAW_ItemSales_PricesSales_Price_Info[]'
error CS0029: Cannot implicitly convert type 'Peachtree.PAW_ItemsPAW_ItemComponentsComponent' to 'Peachtree.PAW_ItemsPAW_ItemComponentsComponent[]'

  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
       at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
       at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
       at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
       at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
       at System.Xml.Serialization.XmlSerializer..ctor(Type type)
       at Sandbox_Console.Program.Main(String[] args) in e:\Code\Sandbox Console\Program.cs:line 16
  InnerException: 

当我试图反序列化文件时,我会得到以下异常

System.InvalidOperationException occurred
  HResult=-2146233079
  Message=There is an error in XML document (4, 3).
  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
       at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
       at Sandbox_Console.Program.Main(String[] args) in e:\Code\Sandbox Console\Program.cs:line 16
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=The specified type was not recognized: name='item', namespace='urn:schemas-peachtree-com/paw8.02-datatypes', at <PAW_Item xmlns=''>.
       Source=Microsoft.GeneratedCode
       StackTrace:
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPAW_Items.Read10_PAW_ItemsPAW_Item(Boolean isNullable, Boolean checkType)
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPAW_Items.Read11_PAW_Items(Boolean isNullable, Boolean checkType)
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderPAW_Items.Read12_PAW_Items()
       InnerException: 

它似乎在 上爆炸了,它不知道如何处理xsi:type=“paw:item”部分。

如果我试图通过将代码更改为paw_itemspaw_item来向已知类型添加paw_itemspaw_item

var types = new[] {typeof (PAW_ItemsPAW_Item)}; //Line 15
var ser = new XmlSerializer(typeof(PAW_Items), types);
var result = (PAW_Items) ser.Deserialize(file);
System.InvalidOperationException occurred
  HResult=-2146233079
  Message=Cannot include anonymous type 'Peachtree.PAW_ItemsPAW_Item'.
  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.XmlReflectionImporter.IncludeType(Type type, RecursionLimiter limiter)
       at System.Xml.Serialization.XmlSerializer..ctor(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, String defaultNamespace, String location, Evidence evidence)
       at System.Xml.Serialization.XmlSerializer..ctor(Type type, Type[] extraTypes)
       at Sandbox_Console.Program.Main(String[] args) in e:\Code\Sandbox Console\Program.cs:line 15
  InnerException: 

我错过了哪一步导致了这种情况的发生?


共有1个答案

邓欣可
2023-03-14

从您得到的第一个错误开始,它是从item.xsd生成代码。Xsd.exe报告所有属性都缺少type属性

<xs:attribute ref="app1:type" />

在复杂类型中。这些属性是由于item.xml文件中的xsi:type属性而生成的。我想您希望XSD.exe生成架构,以便将指定的类型(paw:id,paw:item)用于这些属性。我不知道这是否适用于xsd.exe,您可能需要在xsd.exe上使用其他命令行选项,或者使用其他工具,例如try xsd2code。

 类似资料:
  • 我已经成功地将Allure报表集成到我基于maven的testNG项目中,并能够使用jetty服务器查看报表。但是现在我正试图按照这里建议的指示来整合诱惑报告和詹金斯-http://wiki.qatools.ru/display/al/allure+jenkins+plugin我现在能够在我的詹金斯页面上看到诱惑图标,但是如果我点击这个图标,我会看到一个404-“页面找不到”错误。

  • 总的来说,我对tomcat和JSP很陌生,我还搜索了无数其他问题,这些问题似乎有完全相同的问题,但没有一个能解决我的问题。 服务器结构包含(以及其他内容,如css文件和其他html资源)一个jsp文件和一个java类: jsp文件包含: Java 类的源文件如下所示: 现在,我(重新)启动tomcat服务器,用浏览器访问jsp文件,并得到以下错误消息: 无法编译 JSP 的类:在生成的 java

  • 我正在试图找出为什么我不能在<code>的“消息”: 当我试图解析模式时,我得到了以下错误: 线程“main” org.apache.avro.SchemaParseException中的异常:未定义的名称:“media_type”at org.apache.avro.Schema.parse(Schema.java:1162) at org.apache.avro.Schema.parse(Sc

  • 事情是这样的...我认为注释处理器正在运行,但不知何故,android gradle插件无法在编译时输入生成的源代码。我检查了app/build/generated/source/apt/test/目录,并在其中发现,但由于某种原因,它没有作为依赖项导入。 有什么想法吗?这里是一个测试项目的链接,显示了我的问题

  • 我正在尝试将此框架用于学校项目。为了安装框架,它要求您将zip文件添加到src。在zip文件中,我找到了一个java类和一些AIDL文件。我按照这篇SO帖子上的说明进行操作。当我尝试清理和重建时。我得到一个无法解决符号类错误。我会发布错误日志,但目前我的存储库有问题。在线说明似乎您也需要以某种方式使用APK,但我不知道如何做到这一点。有人知道或能弄清楚如何安装这个框架吗?我将非常感谢任何帮助。

  • 我在IntellijIDEA中有一个项目是用Maven创建的。然后我在pom.xml文件中指定了一组依赖项和外部存储库。 如果我执行“mvn install”,那么项目在命令行上构建得很好。但是,当我打开IDE中的任何代码文件时,它会说Maven依赖项处理的所有类都无法识别--如果我从未将所需的JAR添加到构建路径中,这对于一个正常的项目来说是一样的。 我知道在我的Eclipse Maven项目(