想要从c#中的xml字符串中获取节点 [英] want to get nodes from xml string in c#

查看:384
本文介绍了想要从c#中的xml字符串中获取节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得给定bleow的字符串的内部xml

i want to get the inner xml for the string given bleow

<owl:Thing rdf:about="#CarAdsIns1">
  <hasMake rdf:resource="#MakeIns1"/>
  <hasModel rdf:resource="#ModelIns1"/>
  <hasYear rdf:resource="#YearIns1"/>
  <hasMileage rdf:resource="#MileageIns1"/>
  <hasPrice rdf:resource="#PriceIns1"/>
  <hasPhoneNr rdf:resource="#PhoneNrIns1"/>
</owl:Thing>



代码是:


code is:

private void button1_Click(object sender, EventArgs e)
{
  string s = textBox1.Text;
  XmlDocument xml = new XmlDocument();
  xml.LoadXml(s); // suppose that myXmlString contains "<names>...</names>"

  XmlNodeList xnList = xml.SelectNodes("/owl:Thing");
  foreach (XmlNode xn in xnList)
  {
    textBox1.Text = textBox1.Text + Environment.NewLine + xml.DocumentElement.InnerXml;
  }
}



但是当我运行此代码时出现错误:

owl是未声明的前缀。



我怎么能得到这个内部文本。


but when i am runnign this code m getting error:
owl is an undeclared prefix.

how can i get this inner text.

推荐答案

答案很简单:什么你称XML不是XML 。有效XML的片段不是XML。



要理解此错误,您需要了解XML命名空间的工作原理。在这里,您有前缀未绑定命名空间的情况。您需要名称空间声明,例如

The answer is simple: what you call XML is not XML. A fragment of valid XML is not XML.

To understand this error, you need to understand how XML namespaces work. Here, you have the "prefix not bound with namespace" situation. You need the namespace declarations, such as
<rdf:RDF 

    xmlns     ="http://www.w3.org/TR/2004/REC-owl-guide-20040210/wine#" 

    xmlns:owl ="http://www.w3.org/2002/07/owl#"

    xmlns:rdf ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

    ...

>
   <!- ... -->
</rdf:RDF>



请参阅: http://www.w3.org/TR/owl-guide [ ^ ]。



请注意,前缀本身没有任何意义,可以是与命名空间声明匹配的任何有效唯一前缀名称。请参阅:

http://www.w3.org/TR/REC-xml - 名称 [ ^ ]。



-SA


Please see: http://www.w3.org/TR/owl-guide[^].

Note that prefixes themselves means nothing, the can be any valid unique prefix names matching namespace declarations. Please see:
http://www.w3.org/TR/REC-xml-names[^].

—SA


要读取猫头鹰数据,您需要使用API​​创建适当的对象。这是一个文档 [ ^ ]。
To read owl data, you need to create proper objects using API. Here is a documentation[^].


这篇关于想要从c#中的xml字符串中获取节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆