Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XmlAnyElementAttribute:如何将项目添加到列表<;System.Xml.xmlement>;对于XSD<;任何>;xsd2code创建的类中的元素?_C#_Xsd_Xsd2code - Fatal编程技术网

C# XmlAnyElementAttribute:如何将项目添加到列表<;System.Xml.xmlement>;对于XSD<;任何>;xsd2code创建的类中的元素?

C# XmlAnyElementAttribute:如何将项目添加到列表<;System.Xml.xmlement>;对于XSD<;任何>;xsd2code创建的类中的元素?,c#,xsd,xsd2code,C#,Xsd,Xsd2code,我使用XSD2代码来创建C#类,它可以基于一组XSD文件序列化/反序列化XML文件 一个XSD文件包含一个)实现这一点的唯一方法是一个非常麻烦的方法。首先,在对象中创建所需的结构。序列化该对象并将其用作新XmlDocument的输入,然后将DocumentElement添加到any elementlist 下面是一个可以帮助您的小示例 msg.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:s

我使用XSD2代码来创建C#类,它可以基于一组XSD文件序列化/反序列化XML文件


一个XSD文件包含一个

实现这一点的唯一方法是一个非常麻烦的方法。首先,在对象中创建所需的结构。序列化该对象并将其用作新XmlDocument的输入,然后将DocumentElement添加到any elementlist

下面是一个可以帮助您的小示例

msg.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/msg" xmlns:type="http://tempuri.org/type" targetNamespace="http://tempuri.org/msg" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:import namespace="http://tempuri.org/type" schemaLocation="type.xsd"/>
    <xs:element name="ExampleMsg">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Parent">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Child1" type="xs:string"/>
                            <xs:element name="Child2" type="type:AnyType"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
守则:

static void Main(string[] args)
        {
            var msg = new ExampleMsg {Parent = new ExampleMsgParent {Child1 = "Rob"}};

            var mike = new Mike {Age = 1};
            var john = new John {Age = 2};
            var nick = new Nick {Age = 3};

            var xdoc = new XmlDocument();
            
            xdoc.LoadXml(mike.Serial());
            msg.Parent.Child2.Any.Add(xdoc.DocumentElement);

            xdoc.LoadXml(john.Serial());
            msg.Parent.Child2.Any.Add(xdoc.DocumentElement);

            xdoc.LoadXml(nick.Serial());
            msg.Parent.Child2.Any.Add(xdoc.DocumentElement);

            var msgstring = msg.Serial();
            Console.WriteLine(msgstring);

            Console.ReadLine();
        }
以及输出:

<?xml version="1.0" encoding="utf-8"?>
<ExampleMsg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/msg">
    <Parent>
        <Child1>Rob</Child1>
        <Child2>
            <Mike xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/type">
                <Age>1</Age>
            </Mike>
            <John xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/type">
                <Age>2</Age>
            </John>
            <Nick xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/type">
                <Age>3</Age>
            </Nick>
        </Child2>
    </Parent>
</ExampleMsg>

抢劫
1.
2.
3.
XmlAnyElement can only be used with classes of type XmlNode or a type deriving from XmlNode
[XmlArrayItemAttribute("AnyPlugInContainerType"Namespace = "http://www.extra-standard.de/namespace/plugins/1", Order = 0)]
public List<AbstractPlugInType> Any
{
  get { return _any; }
  set { _any = value; }
}
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/msg" xmlns:type="http://tempuri.org/type" targetNamespace="http://tempuri.org/msg" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:import namespace="http://tempuri.org/type" schemaLocation="type.xsd"/>
    <xs:element name="ExampleMsg">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Parent">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Child1" type="xs:string"/>
                            <xs:element name="Child2" type="type:AnyType"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/type" targetNamespace="http://tempuri.org/type" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:complexType name="AnyType">
        <xs:sequence>
            <xs:any namespace="##targetNamespace" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="Mike">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Age" type="xs:int"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="John">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Age" type="xs:int"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Nick">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Age" type="xs:int"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
@echo off
set exepad=%programfiles(x86)%\Xsd2Code\xsd2code.exe
set sourcepad=%~dp0..\..\

set xsdpad=%sourcepad%ConsoleApp\xsd
set Outpad=%sourcepad%ConsoleApp\Messages

Set NameSpace=ConsoleApp.Messages

"%exepad%"  "%xsdpad%\\type.xsd" /o %Outpad%\type.cs /n %NameSpace% /l cs /pl Net45 /s /xml /sm Serial /dm Deserial /ssp /xa /emt /kro /uct /ust /Indent2Space 
"%exepad%"  "%xsdpad%\\msg.xsd" /o %Outpad%\msg.cs /n %NameSpace% /l cs /pl Net45 /s /xml /sm Serial /dm Deserial /ssp /xa /emt /kro /utc /ust /Indent2Space 

pause
static void Main(string[] args)
        {
            var msg = new ExampleMsg {Parent = new ExampleMsgParent {Child1 = "Rob"}};

            var mike = new Mike {Age = 1};
            var john = new John {Age = 2};
            var nick = new Nick {Age = 3};

            var xdoc = new XmlDocument();
            
            xdoc.LoadXml(mike.Serial());
            msg.Parent.Child2.Any.Add(xdoc.DocumentElement);

            xdoc.LoadXml(john.Serial());
            msg.Parent.Child2.Any.Add(xdoc.DocumentElement);

            xdoc.LoadXml(nick.Serial());
            msg.Parent.Child2.Any.Add(xdoc.DocumentElement);

            var msgstring = msg.Serial();
            Console.WriteLine(msgstring);

            Console.ReadLine();
        }
<?xml version="1.0" encoding="utf-8"?>
<ExampleMsg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/msg">
    <Parent>
        <Child1>Rob</Child1>
        <Child2>
            <Mike xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/type">
                <Age>1</Age>
            </Mike>
            <John xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/type">
                <Age>2</Age>
            </John>
            <Nick xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/type">
                <Age>3</Age>
            </Nick>
        </Child2>
    </Parent>
</ExampleMsg>