简体   繁体   English

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

[英]c# adding xml element where attribute equals

I have an xml file that has a bunch of channels, and I want to append a channel category to every single one of them. 我有一个包含一堆频道的xml文件,我想将频道类别附加到其中的每个频道。 Depending on what channel it is. 取决于它是什么渠道。 I'm very new to this so please excuse me if this is an obvious error. 我对此很陌生,所以如果这是一个明显的错误,请原谅。

example: 例:

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

or 要么

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

I have tried the following: 我尝试了以下方法:

        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;
    }

but it's giving me the following error and I can't figure it out.. What am I doing wrong? 但这给了我以下错误,我无法弄清楚。.我在做什么错?

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

from this line 从这条线

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

My XML looks like this 我的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>

You dont have closing bracket in list/channel[@id='channelname1'(HERE) . 您在list/channel[@id='channelname1'(HERE)没有右括号

Moreover, you are actually trying to do following: 此外,您实际上正在尝试执行以下操作:

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);
    }
}

Why you are using tv instead of list thats why xml library not getting your path of your elements and throwing this error. 为什么要使用电视而不是列表,这就是xml库无法获取元素路径并抛出此错误的原因。

try this.. 尝试这个..

XmlNode node = root.SelectSingleNode("list/channel");
node.Attributes["id"].Value=="channelname1"?true:false;
    bool doesexists = (from data in xmlDoc.Element("tv").Elements("channel")
                   where (string)data.Attribute("id") == channelname
                   select data).Any();

You are trying to reach the channel node where the id equals the channelname inside tv . 您正在尝试到达id等于tvchannel节点。 The problem is that tv does not exist, the channels are inside this: 问题是tv不存在,频道在其中:

<list info="list">

Solution: either put the channels into tv , or use a selector appropriate to your current structure. 解决方案:将频道放入tv ,或使用适合您当前结构的选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM