相关疑难解决方法(0)

XElement命名空间(如何?)

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

<sphinx:docset>
  <sphinx:schema>
    <sphinx:field name="subject"/>
    <sphinx:field name="content"/>
    <sphinx:attr name="published" type="timestamp"/>
 </sphinx:schema>
Run Code Online (Sandbox Code Playgroud)

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

未处理的异常:System.Xml.XmlException:':'字符,十六进制值0x3A,不能包含在名称中.
在System.Xml.XmlConvert.VerifyNCName(String name,ExceptionType exceptionTyp e)
处System.Xml.Linq.XName..ctor(XNamespace ns,String localName)
处于System.Xml.Linq.XNamespace.GetName(String localName)
at System .Xml.Linq.XName.Get(String expandedName)

c# xml linq namespaces

70
推荐指数
2
解决办法
6万
查看次数

将Linq to Xml与Xml命名空间一起使用

我有这个代码:

/*string theXml =
@"<Response xmlns=""http://myvalue.com""><Result xmlns:a=""http://schemas.datacontract.org/2004/07/My.Namespace"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><a:TheBool>true</a:TheBool><a:TheId>1</a:TheId></Result></Response>";*/

string theXml = @"<Response><Result><TheBool>true</TheBool><TheId>1</TheId></Result></Response>";

XDocument xmlElements = XDocument.Parse(theXml);

var elements = from data in xmlElements.Descendants("Result")
               select new {
                            TheBool = (bool)data.Element("TheBool"),
                            TheId = (int)data.Element("TheId"),
                          };

foreach (var element in elements)
{
    Console.WriteLine(element.TheBool);
    Console.WriteLine(element.TheId);
}
Run Code Online (Sandbox Code Playgroud)

当我使用第一个值为xml时,结果为null,而对于第二个值,我有很好的值...

如何使用xmlns值将Linq用于Xml?

c# linq-to-xml xml-namespaces

56
推荐指数
2
解决办法
5万
查看次数

标签 统计

c# ×2

linq ×1

linq-to-xml ×1

namespaces ×1

xml ×1

xml-namespaces ×1