将XML转换为HTML,以更改标签和值的颜色 [英] Convert XML to HTML in order to change colours of tags and values

查看:86
本文介绍了将XML转换为HTML,以更改标签和值的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我只是将XML标签从值中分离出来,但是实际上我需要使标签的颜色与值的颜色不同,并被告知需要将XML代码转换为HTML.尽管我了解XML的基础知识,但我不太熟练.因此,我希望得到一些帮助,就像是从头开始教我一样.

到目前为止,这是我的代码,但由于当前没有子节点,因此也需要更改.

Hi everyone

I just separated my XML tags from my values, but I need to actually make the colours of the tags different to the colours of the values and I was told that I need to convert the XML code to HTML in order to do that. I''m not very fluent with XML although I know the basics, so I would appreciate some assistance as if I was being taught from scratch!

This is my code so far, but it also needs to be changed because at the moment there are no child nodes.

public string Load(string textFromTextbox)
{
   string list = "";
   try
   {
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(textFromTextbox);

      //Get all elements in file and put them in NodeList
      NodeList = doc.GetElementsByTagName( "*" );

      for (int i = 0; i < NodeList.Count; i++)
      {
         list += (NodeList[i].Name + "\t"; + NodeList[i].InnerText + "\n\n";);
      }
   }
   catch ( IOException Ex )
   {
      MessageBox.Show( Ex.Message );
   }
   return list;
}


另外,关于如何在文本框中显示带有多个子项的树结构的任何想法?


Also, any idea of how I can keep the tree structure with the multiple children when it displays in the textbox?

推荐答案

您不必将其更改为XML.获取HtmlAgility库,并使用它.您可以在CodePlex上找到它,而我会留给您谷歌搜索.



You don''t have to change it to XML. Get the HtmlAgility library, and use that. You can find it on CodePlex, and I''ll leave the googling to you.




好的.谢谢...将检查出来.
我创建了一个xslt样式表并尝试在其中进行操作,但是我不知道如何将其链接到XML,因为从上面的代码中可以看到,该XML实际上不是来自文件,而是来自文本框的文本.
我看过一些示例,其中使用物理XML文件(至少就是它的外观)来引用样式表,如下所示:


< asp:删除的id ="XML_ID" runat =服务器" transformsource ="XML_Stylesheet.xslt" xmlns:asp =#unknown">


然后

XML_ID.DocumentContent = RS [0] .ToString();

其中RS是SQL DataReader,我没有,因为这里没有涉及数据库.我可以引用其他任何方式吗?

抱歉,我是新来的! :sigh:
Ah ok. Thanks...will check it out.
I created a xslt stylesheet and tried to do it there, but i dont know how to link it to the XML because as you can see from my code above, the XML isn''t actually from a file, its the text from a textbox.
I have seen samples where a physical XML file (at least thats what it looks like) was used to reference the stylesheet like this:


<asp:removed id="XML_ID" runat="server" transformsource="XML_Stylesheet.xslt" xmlns:asp="#unknown">


then

XML_ID.DocumentContent = RS[0].ToString();

where RS is a SQL DataReader, which i dont have since there is no database involved here. Any other ways i can reference it?

Sorry, im new at this! :sigh:


找到了答案!实际上,我首先不需要在XML类中使用Load()方法.
我创建了一个事件处理程序文件(.ashx),并简单地将其更改为返回一条消息,即XML字符串.
因此,在form_load中,我有

string msg =带有\的整个xml字符串,以转引号";
context.Response.ContentType ="application/xml";
context.Response.Write(msg);

这就是整个文件中的所有代码!因此,当您运行该应用程序时,请在浏览器的"URL"框中直接运行此文件.
您将得到的是一种XML,就像我们程序员看到的那样...具有整个树结构,红色标记和黑色值.

此方法不使用.xslt,但我目前正在使用.xslt引用来获取要显示的数据.这样,您将在表视图而不是XML视图中查看数据.

关于表视图中的父母和孩子……它们将被编号.因此,如果父母是1.1,则孩子将是1.1.1、1.1.2等
Found the answer! I actually didnt need a Load() method in my XML class in the first place.
I created an Event Handler file (.ashx), and simply changed it to return a message, which is the XML string.
So, inside the form_load, i have

string msg = "the whole xml string with \ to escape the quotes";
context.Response.ContentType = "application/xml";
context.Response.Write(msg);

And thats all the code in that whole file! So when you run the app, in the URL box in your browser, run this file directly.
What you will get is an XML the way us programmers see it...with the whole tree structure, and with the red tags and black values.

This method does not use the .xslt, but i am currently working on getting the data to display using the .xslt reference. With that, you will see the data in a table view rather than XML view.

With regards to parents and children in the table view...they will be numbered. So if a parent is 1.1, the child will be under that as 1.1.1, 1.1.2 etc


这篇关于将XML转换为HTML,以更改标签和值的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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