如何使用多个值动态创建XSI:Schemalocation

本文关键字:创建 XSI Schemalocation 动态 何使用 | 更新日期: 2023-09-27 17:54:20

大家好,我正在动态创建一个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);

谁来帮帮我

如何使用多个值动态创建XSI:Schemalocation

只有"仅在您的应用程序中拥有它们"的信息,我不能为您提供任何更动态的信息,但假设您可以使用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属性的值。