微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在XML中插入xsi:schemaLocation和xlmns和前缀

如何解决在XML中插入xsi:schemaLocation和xlmns和前缀

我正在尝试将xsi和xsmln数据插入XML,我使用了多种方法,但是我无法获得它,我还需要插入前缀。接下来,我展示我的代码在哪里获得XML,还展示给我我得到的结构,最后,展示给我我真正想要的东西。 这是我获取XML的代码

string txtXML = Xmlfrommyfunctionsql();    // here retrieve from sqlserver          
            XDocument doc;
            using (StringReader s = new StringReader(txtXML))
            {
                doc = XDocument.Load(s);
            }
            doc.Declaration = new XDeclaration("1.0","UTF-8",null);
            string targetxml = doc.ToString();
            targetxml = doc.Declaration.ToString() + Environment.NewLine + doc.ToString();

这是我在字符串targetxml中获得的XML:

     <?xml version="1.0" encoding="UTF-8"?>
<Invoice>
    <UBLxtensions>
        <UBLExtension>
            <AccountingsupplierParty>
                <AdditionalAccountID>1</AdditionalAccountID>
                <Party>
                    <PartyName>
                        <Name>GRUPO ERB</Name>
                    </PartyName>
                    <PhysicalLocation>
                        <Address>
                            <ID>11001</ID>
                            <Country>
                                <IdentificationCode>CO</IdentificationCode>
                            </Country>
                        </Address>
                    </PhysicalLocation>
                </Party>
            </AccountingsupplierParty>
        </UBLExtension>
    </UBLExtensions>
</Invoice>

       

但是我需要插入xsi:schemaLocation和xmlns,并在Elemnts中插入前缀,我该怎么做? 我希望得到这个:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice Invoice xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 
    http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#" 
    xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" 
    xmlns:sts="http://www.dianees.com/contra/acturaeca/v1/Structures" 
    xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <cac:AccountingsupplierParty>
                <cbc:AdditionalAccountID>1</cbc:AdditionalAccountID>
                    <cac:Party>
                        <cac:PartyName>
                            <cbc:Name>GRUPO ERB</cbc:Name>
                        </cac:PartyName>
                        <cac:PhysicalLocation>
                            <cac:Address>
                                <cbc:ID>11001</cbc:ID>
                                <cac:Country>
                                    <cbc:IdentificationCode>CO</cbc:IdentificationCode>
                                </cac:Country>
                            </cac:Address>
                        </cac:PhysicalLocation>
                </cac:Party>
            </cac:AccountingsupplierParty>
        </ext:UBLExtension>
    </ext:UBLExtensions>
</Invoice>

请告诉我如何制作一个,其余的我会做

解决方法

这将为您提供解决问题的方法。我创建了一个小例子。它远非完美,但基本显示​​了应采取的步骤。

首先将无名称空间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="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Parent">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Child1" type="xs:string"/>
                            <xs:element name="Child2" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

然后,由于您消息中的命名空间不同,因此我创建了具有不同命名空间的相同消息。每个命名空间都是一个不同的文件。

Root.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/Root" xmlns:p="http://tempuri.org/Parent" targetNamespace="http://tempuri.org/Root" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:import namespace="http://tempuri.org/Parent" schemaLocation="Parent.xsd"/>
    <xs:element name="Root" type="p:ParentType"/>
</xs:schema>

parent.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/Parent" xmlns:ch="http://tempuri.org/Child" targetNamespace="http://tempuri.org/Parent" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:import namespace="http://tempuri.org/Child" schemaLocation="Child.xsd"/>
    <xs:complexType name="ParentType">
        <xs:sequence>
            <xs:element name="Parent" type="ch:ChildType"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Child.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns="http://tempuri.org/Child1" targetNamespace="http://tempuri.org/Child">
    <xs:complexType name="ChildType">
        <xs:sequence>
            <xs:element name="Child1" type="xs:string"/>
            <xs:element name="Child2" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

下一步是使用xsd.exe创建类文件。我使用bat文件执行te命令。

@echo off
set exepad=C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\xsd.exe
set sourcepad=..\..\

set xsdpad=%sourcepad%\console\xsd
set Outpad=%sourcepad%\console\generated

Set NameSpace=Console.generated

"%exepad%" "%xsdpad%\NoNSmessage.xsd"  /c  /n:%NameSpace%  /o:%Outpad%

"%exepad%" "%xsdpad%\Child.xsd" "%xsdpad%\Parent.xsd" "%xsdpad%\Root.xsd"  /c  /n:%NameSpace%  /o:%Outpad%

pause

然后将生成的类文件导入您的项目,以便可以使用它们。

这是一个示例,您可以将字段从源映射到目标。

static void Main(string[] args)
        {
            const string msg1 = "<Root><Parent><Child1>First</Child1><Child2>Second</Child2></Parent></Root>";

            //deserialize xml string into class object.
            XmlSerializer deserializer = new XmlSerializer(typeof(Root));
            var reader = new StringReader(msg1);
            var noNamespaceRoot = (Root)deserializer.Deserialize(reader);

            //map the fields from the nonamespace msg to the msg with a namespace
            var namespaceRoot = new ParentType();

            namespaceRoot.Parent = new ChildType();
            namespaceRoot.Parent.Child1 = noNamespaceRoot.Parent.Child1;
            namespaceRoot.Parent.Child2 = noNamespaceRoot.Parent.Child2;

            //serialize the class object to a string.
            var serializer = new XmlSerializer(typeof(ParentType));
            var sww = new StringWriter();
            using (XmlWriter writer = XmlWriter.Create(sww))
            {
                serializer.Serialize(writer,namespaceRoot);
            }
            System.Console.WriteLine(sww.ToString());

        }

输出将是:

<?xml version="1.0" encoding="utf-16"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/Root">
    <Parent xmlns="http://tempuri.org/Parent">
        <Child1 xmlns="http://tempuri.org/Child">First</Child1>
        <Child2 xmlns="http://tempuri.org/Child">Second</Child2>
    </Parent>
</Root>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。