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

insert xsi:schemaLocation and xlmns and prefix in XML

我正在尝试将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 创建 class 文件。我使用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

然后将生成的 class 文件导入到您的项目中以便使用。

这是一个如何将字段从源映射到目标的示例。

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>