我是Xdocument的新手,我需要你的帮助才能理解我做错了什么。
我有以下代码:
static void Main(string[] args)
{
XNamespace ns = "http://somesite.com";
XNamespace schemalocation = "http://somesite/schema.xsd";
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace Xmlns = "http://www.w3.org/2001/XMLSchema-instance";
var date = DateTime.Now;
var guid = "urn:uuid:" + Guid.NewGuid();
XDocument doc = new XDocument(
new XElement(ns + "Message",
new XAttribute("id", guid),
new XAttribute("messageType", Properties.Settings.Default.messageType),
new XAttribute("dateTime", date),
new XAttribute("origin", Properties.Settings.Default.origin),
new XAttribute("originType", Properties.Settings.Default.originType),
new XAttribute("destination", Properties.Settings.Default.destination),
new XAttribute("userName", Properties.Settings.Default.userName),
new XAttribute("xmlns", ns.NamespaceName),
new XAttribute(XNamespace.Xmlns + "xsi", Xmlns),
new XAttribute(xsi + "schemaLocation", schemalocation),
new XElement ("Query",
new XElement("WhereClause", Properties.Settings.Default.WhereClause),
new XElement("ReturnStructure", Properties.Settings.Default.ReturnStructure)
)));
当我保存xml时,我得到一个空的 xmlns ,用于子元素“Query”
<Query xmlns="">
我需要对代码进行哪些更改才能获得没有命名空间的“查询”,因此它看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<Message id="urn:uuid:4ed742cf-1ee4-4548-9105-c230933cf473" messageType="Type" dateTime="datetime" origin="service" originType="type" destination="destination" userName="username" xmlns="http://somesite.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://somesite/schema.xsd">
<Query>
<WhereClause>somequery</WhereClause>
<ReturnStructure>somereturn</ReturnStructure>
</Query>
</Message>
感谢您的帮助
圣拉斐尔
答案 0 :(得分:0)
您需要将父命名空间添加到Query标记和Query标记的子项
new XElement (ns + "Query")
new XElement (ns + "WhereClause")
new XElement (ns + "ReturnStructure")
因为一旦你将命名空间添加到Query,子标签将没有命名空间,它们将显示空命名空间标签。