猿问

XElement命名空间(如何?)

如何使用节点前缀创建xml文档,例如:


<sphinx:docset>

  <sphinx:schema>

    <sphinx:field name="subject"/>

    <sphinx:field name="content"/>

    <sphinx:attr name="published" type="timestamp"/>

 </sphinx:schema>

当我尝试运行类似new XElement("sphinx:docset")我得到异常的东西时


未处理的异常:System.Xml.XmlException:名称中不能包含':'字符,十六进制值0x3A。

在System.Xml.Linq.XName..ctor(

XNamespace ns,String localName)

在System.Xml.Linq.XNamespace.GetName(String localName)

在System.Xml.Xml.XmlConvert.VerifyNCName(String name,ExceptionType exceptionTyp e).Xml.Linq.XName.Get(字符串expandName)


婷婷同学_
浏览 799回答 2
2回答

HUH函数

您可以读取文档的名称空间,并在如下查询中使用它:XDocument xml = XDocument.Load(address);XNamespace ns = xml.Root.Name.Namespace;foreach (XElement el in xml.Descendants(ns + "whateverYourElementNameIs"))&nbsp; &nbsp; //do stuff
随时随地看视频慕课网APP
我要回答