不完整的XML属性 [英] Incomplete XML attribute
问题描述
我正在通过dataset.GetXML()方法从数据集中创建XML。
我要向其中添加属性
I am creating XML out of Dataset by dataset.GetXML() method. I want to add attributes to it
XmlAttribute attr = xmlObj.CreateAttribute("xmlns:xsi");
attr.Value = "http://www.createattribute.com";
xmlObj.DocumentElement.Attributes.Append(attr);
attr = xmlObj.CreateAttribute("xsi:schemaLocation");
attr.Value = "http://www.createattribute.com/schema.xsd";
xmlObj.DocumentElement.Attributes.Append(attr);
xmlObj.DocumentElement.Attributes.Append(attr);
但是当我打开XML文件时,发现 xsi: schemaLocation的属性中没有
But when I open the XML file, I found "xsi:" was not there in the attribute for schemaLocation
<root xmlns="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.createattribute.com"
schemaLocation="http://www.createattribute.com/schema.xsd">
我想要
xsi:schemaLocation="http://www.createattribute.com/schema.xsd"
这总是像这样,还是我在这里错过了一些东西
我很好奇是否有人可以帮助我解决这个问题,或者在我可以找到解决方案的时候给我一些URL
Is this always like this, or i m missing something here. I am curious if anyone could help me if this could be resolved or give me some URL when I can find the solution for this
谢谢
推荐答案
此处的关键是,您需要告诉XmlWriter使用哪些名称空间,然后从那里使用正确的前缀。
The key here is that you need to tell the XmlWriter what namespaces to use and from there it will apply the correct prefixes.
在SetAttribute方法的第二个参数下面的代码中,是为xmlns:xsi命名空间指定的命名空间uri。
In the code below the second parameter in the SetAttribute method is the namespace uri specified for xmlns:xsi namespace. This lets the XmlWrite put in the right prefix.
XmlDocument xmlObj = new XmlDocument();
xmlObj.LoadXml("<root></root>");
XmlElement e = xmlObj.DocumentElement;
e.SetAttribute("xmlns:xsi", "http://www.createattribute.com");
e.SetAttribute("schemaLocation", "http://www.createattribute.com", "http://www.createattribute.com/schema.xsd");
使用原始问题的语法的类似代码是:
Similar code using the syntax from your original question is:
XmlDocument xmlObj = new XmlDocument();
xmlObj.LoadXml("<root></root>");
XmlAttribute attr = xmlObj.CreateAttribute("xmlns:xsi");
attr.Value = "http://www.createattribute.com";
xmlObj.DocumentElement.Attributes.Append(attr);
attr = xmlObj.CreateAttribute("schemaLocation", "http://www.createattribute.com");
attr.Value = "http://www.createattribute.com/schema.xsd";
xmlObj.DocumentElement.Attributes.Append(attr);
这篇关于不完整的XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!