如何创建XSI:使用多个值动态进行构建

时间:2013-01-18 07:03:05

标签: c# asp.net xml

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

有人可以帮助我

1 个答案:

答案 0 :(得分: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属性的值。