简体   繁体   English

如何使用 C# 在 XML 文件中编写符号“<”>

[英]How to write symbols "<" ">" in a XML File with C#

I need help with my task, right now i'm developing a website with C# and I need to create XML labels automatically, of a text in a special label, right I get all the data and i add it in an arrays and i create all the sentence, right now i'm fine with my result.我需要帮助完成我的任务,现在我正在用 C# 开发一个网站,我需要自动创建 XML 标签,在特殊标签中的文本,正确的我获取所有数据并将其添加到数组中并创建所有的句子,现在我对我的结果很好。

My result is like these lines:我的结果就像这些行:

double M = <M>2.0</M>
double C = <C>0.59</C>
double D = <D>0.48</D>
double E = <E>0.69</E>

But right now my problem is the next, when i try to save the information to the XML file, I have two issues.但现在我的问题是下一个,当我尝试将信息保存到 XML 文件时,我有两个问题。

1.- I cant write the declaration of the variables between labes. 1.- 我不能写实验室之间的变量声明。 2.- When i save the information in to the XML, the symbols of "<" and ">", has change for ( &gt; and &lt; ). 2.- 当我将信息保存到 XML 中时,"<" 和 ">" 的符号已更改为 ( &gt;&lt; )。

There is a way to save the result:有一种方法可以保存结果:

double M = <M>2.0</M>
double C = <C>0.59</C>
double D = <D>0.48</D>
double E = <E>0.69</E>

Or these it's imposible?或者这些是不可能的?

Thanks for your time谢谢你的时间

Is this what you want?这是你想要的吗?

<root><![CDATA[
double M = <M>2.0</M>
double C = <C>0.59</C>
double D = <D>0.48</D>
double E = <E>0.69</E>
]]></root>

Then do this in your code:然后在您的代码中执行此操作:

XElement root = new XElement("root", new XCData(@"
double M = <M>2.0</M>
double C = <C>0.59</C>
double D = <D>0.48</D>
double E = <E>0.69</E>
"));

I'm not really sure what your issue is here - presumably it's something to do with how you're putting the XML together.我不太确定您的问题是什么 - 大概与您如何将 XML 放在一起有关。 I've just knocked this up quickly and it works just fine:我刚刚快速解决了这个问题,它工作得很好:

class Program
{
    static string GetData()
    {
        return "double M = <M>2.0</M>" +
               "double C = <C>0.59</C>" +
               "double D = <D>0.48</D>" +
               "double E = <E>0.69</E>";
    }

    static void Main(string[] args)
    {
        XmlDocument doc = new XmlDocument();
        var variablesAndFunctions = doc.CreateElement("VariablesAndFunctions");
        doc.AppendChild(variablesAndFunctions);

        var constraints = doc.CreateElement("Constraints");
        constraints.InnerXml = GetData();
        variablesAndFunctions.AppendChild(constraints);

        Console.WriteLine(doc.OuterXml);
        Console.ReadLine();
    }
}

The key line is setting the InnerXml property of an element to the Xml content that was returned from the GetData function.关键是将元素的InnerXml属性设置为从GetData函数返回的 Xml 内容。 Should work fine for you too.应该也适合你。

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

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