如何使用C#替换XSD文件中的文本?

时间:2019-03-04 10:50:46

标签: c# xml xsd

我想用新的枚举替换字母枚举。 这是xsd文件,其中还包含其他数据 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="Vehicle">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Ford" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Alphabets" minOccurs="0" maxOccurs="1">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="a" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                    <xs:element name="b" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                    <xs:element name="c" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                        <xs:element name="SectionB" minOccurs="0" maxOccurs="1">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="BX010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                    <xs:element name="BX070_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                    <xs:element name="BX350N_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                    <xs:element name="BX350NNOTE_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>

                         </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

这是我的c#代码,正在读取文件并将其保存在ReturnReasonsXMLFromFile中,然后将整个xsd替换为新的xml。 我只想用xml中给出的任何内容替换Alphabets的特定元素。

  private static void WriteFile(string filePath, string xml)
    {
        try
        {
            if (File.Exists(filePath))
            {
                string ReturnReasonsXMLFromFile = File.ReadAllText(filePath);
                if (!String.Equals(xml, ReturnReasonsXMLFromFile))
                {
                    if(ReturnReasonsXMLFromFile.Contains("itemReturnReasonType"))
                    {
                        //datatypexs = element.Attribute("type").Value;
                    }
                    //File.Delete(filePath);
                    //File.WriteAllText(filePath, xml);
                }
            }
            else
                File.WriteAllText(filePath, xml);
        }
        catch (Exception exception)
        {
            ShowException(exception);
        }
    }

预期产量

 <?xml version="1.0" encoding="utf-8"?>
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:element name="Vehicle">
<xs:complexType>
    <xs:sequence>
        <xs:element name="Ford" minOccurs="0" maxOccurs="1">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="Alphabets" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="x" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                <xs:element name="y" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                <xs:element name="z" type="Dollar" minOccurs="0" maxOccurs="1"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="SectionB" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="BX010_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                <xs:element name="BX070_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                <xs:element name="BX350N_1" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                <xs:element name="BX350NNOTE_1" type="ExplanatoryText" minOccurs="0" maxOccurs="1"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>

                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

XML:

   <xs:sequence>
                    <xs:element name="Alphabets" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="x" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                <xs:element name="y" type="Dollar" minOccurs="0" maxOccurs="1"/>
                                <xs:element name="z" type="Dollar" minOccurs="0" maxOccurs="1"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>

0 个答案:

没有答案