0

大家好,我正在动态创建一个 XML 文件,我遇到了以下问题,我有以下代码,它将分配架构位置,如下所示

XmlAttribute attr5 = doc.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
 string strXSDPath = "http://www.irs.gov/efile ";
 strXSDPath = strXSDPath + Server.MapPath("ReturnData941.xsd");
 attr5.Value = strXSDPath;
 returnData.Attributes.Append(attr5);

这工作正常,这在我的 XML 文件中给出如下

<?xml version="1.0" encoding="UTF-8"?>
<SOAP:Envelope xmlns="http://www.irs.gov/efile" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:efile="http://www.irs.gov/efile" 
 ***xsi:schemaLocation="http://www.irs.gov/efile ReturnData941.xsd*"** />

但是在这里,我想在单个中具有多个模式位置,如下所示

xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/../message/SOAP.xsd
               http://www.irs.gov.efile../message/efileMessage.xsd"

这是我的 XML 生成代码

XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);

            //XmlNode returnData = doc.CreateElement("SOAP");

            XmlElement returnData = doc.CreateElement("SOAP", "Envelope", "http://www.irs.gov/efile");



            XmlAttribute attr = doc.CreateAttribute("xmlns");
            attr.Value = "http://www.irs.gov/efile";
            returnData.Attributes.Append(attr);

            XmlAttribute attr1 = doc.CreateAttribute("xmlns:xsi");
            attr1.Value = "http://www.w3.org/2001/XMLSchema-instance";
            returnData.Attributes.Append(attr1);


            XmlAttribute attr3 = doc.CreateAttribute("xmlns:SOAP");
            attr3.Value = "http://schemas.xmlsoap.org/soap/envelope/";
            returnData.Attributes.Append(attr3);

            XmlAttribute attr4 = doc.CreateAttribute("xmlns:efile");
            attr4.Value = "http://www.irs.gov/efile";
            returnData.Attributes.Append(attr4);

            XmlAttribute attr5 = doc.CreateAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
            attr5.Value = strXSDPath;
            returnData.Attributes.Append(attr5);

所以有人可以帮助我吗

4

1 回答 1

0

只有您“仅在您的应用程序中拥有它们”的信息,我无法为您提供更多动态的信息,但假设您可以使用 Server.MapPath() 获取所有文件路径,这应该可以获取值你要:

Dictionary<string, string> pairs = new Dictionary<string, string>
{
    {"http://www.irs.gov/efile","ReturnData941.xsd"},
    {"http://schemas.xmlsoap.org/soap/envelope/", "SOAP.xsd"},
    // add as many more of these as you need
};
string schemaLocationValue = 
  string.Join(" ", pairs.Select(p => p.Key + " " + Server.MapPath(p.Value)).ToArray());

然后,您只需使用该schemaLocationValue字符串作为schemaLocation属性的值。

于 2013-01-18T07:31:10.187 回答