没有命名空间的 C# XElement
C# XElement without namespace
我是 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 时,子元素 "Query"
得到一个空的 xmlns
<Query xmlns="">
我需要对我的代码做哪些更改才能在没有命名空间的情况下获得 "Query",因此它看起来像这样:
<?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>
感谢您的帮助
拉斐尔
您需要将父命名空间添加到查询标记和查询标记的子项中
new XElement (ns + "Query")
new XElement (ns + "WhereClause")
new XElement (ns + "ReturnStructure")
因为一旦您将命名空间添加到 Query 中,子标签将没有命名空间,它们将显示为空的命名空间标签。
我是 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 时,子元素 "Query"
得到一个空的 xmlns<Query xmlns="">
我需要对我的代码做哪些更改才能在没有命名空间的情况下获得 "Query",因此它看起来像这样:
<?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>
感谢您的帮助
拉斐尔
您需要将父命名空间添加到查询标记和查询标记的子项中
new XElement (ns + "Query")
new XElement (ns + "WhereClause")
new XElement (ns + "ReturnStructure")
因为一旦您将命名空间添加到 Query 中,子标签将没有命名空间,它们将显示为空的命名空间标签。