c#在属性等于的地方添加xml元素

时间:2016-08-22 06:24:20

标签: c# .net xml sqlite parsing

我有一个包含多个频道的xml文件,我想在每个频道中添加一个频道类别。取决于它是什么渠道。我对此非常陌生,所以如果这是一个明显的错误,请原谅我。

示例:

<channel-category>Entertainment</channel-category>

<channel-category>News</channel-category>

我尝试了以下内容:

        string path;
        string xmlfile = "/channels.xml";
        path = Environment.CurrentDirectory + xmlfile;

        if (exists("channelname1"))
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNode root = doc.DocumentElement;
            XmlNode node = root.SelectSingleNode("list/channel[@id='channelname1'");
            XmlNode category = doc.CreateElement("channel-category");
            category.InnerText = "channelcataegorygoeshere";
            node.AppendChild(category);
            doc.DocumentElement.AppendChild(node);
        }
        else
        {
            Console.WriteLine("not found");
        }
        Console.ReadKey();
    }
    static bool exists(string channelname)
    {
        string path;
        string xmlfile = "/channels.xml";
        path = Environment.CurrentDirectory + xmlfile;
        XDocument xmlDoc = XDocument.Load(path);

        bool doesexists = (from data in xmlDoc.Element("list").Elements("channel")
                       where (string)data.Attribute("id") == channelname
                       select data).Any();
        return doesexists;
    }

但是它给了我以下错误,我无法弄清楚..我做错了什么?

 An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll
    Additional information: 'list/channel[@id='channelname1'' has an invalid token.

从这一行开始

XmlNode node = root.SelectSingleNode("list/channel[@id='channelname1'");

我的XML看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<list info="list">
  <channel id="channelname1">
    <display-name lang="en">channelname1</display-name>
    <icon src="http://locationtologo.com/" />
    <url>http://someurl.com</url>
  </channel>
  <channel id="channelname2">
    <display-name lang="en">channelname2</display-name>
    <icon src="http://locationtologo.com/" />
    <url>http://someurl.com</url>
  </channel>
  <channel id="channelname3">
    <display-name lang="en">channelname3</display-name>
    <icon src="http://locationtologo.com/" />
    <url>http://someurl.com</url>
  </channel>
  <channel id="channelname4">
    <display-name lang="en">channelname4</display-name>
    <icon src="http://locationtologo.com/" />
    <url>http://someurl.com</url>
  </channel>
</list>

3 个答案:

答案 0 :(得分:1)

list/channel[@id='channelname1'(HERE)中没有右括号

此外,您实际上是在尝试执行以下操作:

var doc = new XmlDocument();
doc.Load(Environment.CurrentDirectory + "\\channels.xml");
var nodes = doc.SelectNodes("list/channel[@id=\"channelname1\"]");
if (nodes != null)
{
    foreach (XmlNode node in nodes)
    {
        var el = doc.CreateElement("channel-category");
        el.InnerText = "SomeValue";
        node.AppendChild(el);
    }
}

答案 1 :(得分:0)

为什么你使用tv而不是list,这就是为什么xml库没有得到你的元素路径并抛出这个错误。

试试这个..

XmlNode node = root.SelectSingleNode("list/channel");
node.Attributes["id"].Value=="channelname1"?true:false;

答案 2 :(得分:0)

    bool doesexists = (from data in xmlDoc.Element("tv").Elements("channel")
                   where (string)data.Attribute("id") == channelname
                   select data).Any();

您正试图访问channel节点,其中id等于tv内的频道名称。问题是tv不存在,渠道在这里:

<list info="list">

解决方案:要么将频道放入tv,要么使用适合当前结构的选择器。