C# 在XML中插入xsi:schemaLocation和xlmns以及前缀

C# 在XML中插入xsi:schemaLocation和xlmns以及前缀,c#,xml,xml-namespaces,C#,Xml,Xml Namespaces,我正在尝试将xsi和xsmln数据插入到XML中,我使用了各种方法,但我无法获得它,我还需要插入前缀。接下来,我将展示我的代码,从哪里获取XML,我还将向您展示我获取的结构,最后我将向您展示我真正想要的内容。 这是我获取XML的代码: string txtXML = XmlfrommyfunctionSQL(); // here retrieve from sqlserver XDocument doc; using (S

我正在尝试将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>

       

1.
GRUPO ERB
11001
一氧化碳
但我需要插入xsi:schemaLocation和xmlns,并在元素中插入前缀,我该怎么做? 我希望得到这个:

<?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>

1.
GRUPO ERB
11001
一氧化碳

请告诉我如何做一个,我会做剩下的

这会给你一个解决问题的方法。我创建了一个小例子。它远非十全十美,但基本上显示了应该采取哪些步骤

首先,无名称空间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>
然后将生成的类文件导入到项目中,以便可以使用它们

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

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());

        }
static void Main(字符串[]args)
{
常量字符串msg1=“FirstSecond”;
//将xml字符串反序列化为类对象。
XmlSerializer反序列化器=新的XmlSerializer(typeof(Root));
变量读取器=新的StringReader(msg1);
var noNamespaceRoot=(根)反序列化器。反序列化(读取器);
//将非命名空间消息中的字段映射到具有命名空间的消息
var namespaceRoot=新的父类型();
namespaceRoot.Parent=新的子类型();
namespaceRoot.Parent.Child1=非namespaceRoot.Parent.Child1;
namespaceRoot.Parent.Child2=非namespaceRoot.Parent.Child2;
//将类对象序列化为字符串。
var serializer=新的XmlSerializer(typeof(ParentType));
var sww=新的StringWriter();
使用(XmlWriter=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>

弗斯特
第二

您想要的东西可以完成,但需要大量不必要的编码。我认为最好创建两个xml模式定义,一个匹配原始sql输出,另一个匹配您希望的外观。然后使用诸如xsd.exe或xsd2code.exe之类的工具为这些文件生成类。将SQLXML输出组合到类中,创建一个映射,在该映射中用名称空间填充类。当完成序列化后。同意,看一看XSD2代码,可能会给你一个如何从不同的方法解决这个问题的想法谢谢Martijn,但我不明白你关于创建两个方案的意思,我是这方面的初学者,你能更明确一点吗,也许你能给我展示一个如何使用xsd.exe的示例,我很感激…嗨,martijn,我刚刚使用了xsd.exe并生成了一个类,这个类非常大,因为我需要使用7个xsd文件。现在,如何从该类生成XML?
<?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>
@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>