【问题标题】:Creating XMLDocument throguh code in asp.net在 asp.net 中通过代码创建 XML 文档
【发布时间】:2013-10-15 17:38:59
【问题描述】:

我正在尝试通过代码生成这样的 XML 文档。

<TestRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://localhost:2292/RMSchema.xsd">
    <Version>3</Version>
    <ApplicationHeader>
        <AppLanguage />
        <UserId>rmservice</UserId>
    </ApplicationHeader>
    <CustomerData>
        <ExistingCustomerData>
            <MTN>2084127182</MTN>
        </ExistingCustomerData>
    </CustomerData>
</TestRequest>

我尝试了一些样品。但是他们为孩子们创建了 xmlns,我不需要。非常感谢任何帮助。

我已经尝试了下面的代码。但它只向所有孩子添加 xmlns,我不需要

XmlDocument xDocument = new XmlDocument();
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null));
XmlElement xRoot = xDocument.CreateElement("TestRequest", "XNamespace.Xmlns=http://www.w3.org/2001/XMLSchema-instance" + " xsi:noNamespaceSchemaLocation=" + "http://localhost:2292/RMSchema.xsd");
xDocument.AppendChild(xRoot);
xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = 1;

谢谢 兔兔

我试过了

var xsi = "http://www.w3.org/2001/XMLSchema-instance";
            XmlElement xRoot = xDocument.CreateElement("xsi","RMRequest",xsi);
            xRoot.SetAttribute("noNamespaceSchemaLocation", xsi, "http://localhost:2292/RMSchema.xsd");

            xDocument.AppendChild(xRoot);
Now the response is 

<?xml version=\"1.0\" encoding=\"windows-1252\"?><xsi:TestRequest xsi:noNamespaceSchemaLocation=\"http://localhost:2292/RMSchema.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">

【问题讨论】:

  • 你确定需要使用 XmlDocument 吗? LINQ to XML 通常更简单。此外,您应该展示您已经尝试过的内容,以便我们尝试帮助您解决问题。

标签: c# asp.net xml xmldocument


【解决方案1】:

这是很棒的 LINQ to XML。尽情享受吧!

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XDocument doc = new XDocument(new XDeclaration("1.0", "windows-1252", null),  
    new XElement("TestRequest",
        new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
        new XAttribute(xsi + "noNamespaceSchemaLocation", "http://localhost:2292/RMSchema.xsd"),
        new XElement("Version",
                new XText("3")
        ),
        new XElement("ApplicationHeader",
                new XElement("AppLanguage"),
                new XElement("UserId",
                        new XText("rmservice")
                )
        ),
        new XElement("CustomerData",
            new XElement("ExistingCustomerData",
                new XElement("MTN", 
                    new XText("2084127182")
                )
            )
        )
    )
);

doc.Save(filePath);

如果你真的想要旧的 API,这里是:

var xDocument = new XmlDocument();
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null));

var xsi = "http://www.w3.org/2001/XMLSchema-instance";
var xRoot = xDocument.CreateElement("TestRequest");

var attr = xDocument.CreateAttribute("xsi", "noNamespaceSchemaLocation", xsi);
attr.Value = "http://localhost:2292/RMSchema.xsd";
xRoot.Attributes.Append(attr);

xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = "1";

// ..  your other elemets ...

xDocument.AppendChild(xRoot);
xDocument.Save(filePath);

编辑:从您的 cmets 来看,您似乎希望 xmlns:xsi 和其他属性按特定顺序排列。如果是这样,您可能必须先欺骗XmlDocument 添加xmlns:xsi 属性。

var xDocument = new XmlDocument();
xDocument.AppendChild(xDocument.CreateXmlDeclaration("1.0", "windows-1252", null));

var xsi = "http://www.w3.org/2001/XMLSchema-instance";
var xRoot = xDocument.CreateElement("TestRequest");

// add namespace decl are attribute
var attr = xDocument.CreateAttribute("xmlns:xsi");
attr.Value = xsi;
xRoot.Attributes.Append(attr);

// no need to specify prefix, XmlDocument will figure it now
attr = xDocument.CreateAttribute("noNamespaceSchemaLocation", xsi);
attr.Value = "http://localhost:2292/RMSchema.xsd";
xRoot.Attributes.Append(attr);

xRoot.AppendChild(xDocument.CreateElement("Version")).InnerText = "1";

// ..  your other elemets ...

xDocument.AppendChild(xRoot);
xDocument.Save(filePath);

【讨论】:

  • 现在得到 localhost:2292/RMSchema.xsd\" xmlns:xsi= \"w3.org/2001/XMLSchema-instance\">
  • var xsi = "w3.org/2001/XMLSchema-instance"; XmlElement xRoot = xDocument.CreateElement("xsi","RMRequest",xsi); xRoot.SetAttribute("noNamespaceSchemaLocation", xsi, "localhost:2292/RMSchema.xsd"); xDocument.AppendChild(xRoot); 现在 m
  • 我已经编辑了我的答案一段时间,以删除将xsi 添加到根元素的错误。刷新你的页面,看看我的回答。
  • 现在得到 localhost:2292/RMSchema.xsd\" xmlns:xsi=\"w3.org/2001/XMLSchema-instance\"> 我可以得到它 localhost:2292/RMSchema.xsd\" >
  • 当然,把var xRoot = xDocument.CreateElement("TestRequest");改成var xRoot = xDocument.CreateElement("RMRequest");
【解决方案2】:

你在寻找这样的东西吗:-

XmlDocument xmldoc = new XmlDocument();
XmlNode root = xmldoc.AppendChild(xmldoc.CreateElement("Root"));
XmlNode child = root.AppendChild(xmldoc.CreateElement("Child"));
XmlAttribute childAtt =child.Attributes.Append(xmldoc.CreateAttribute("Attribute"));
childAtt.InnerText = "My innertext";
child.InnerText = "My node Innertext";
xmldoc.Save("ABC.xml");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2021-08-29
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多