在XML中添加没有xml元素的纯文本

时间:2017-06-16 05:34:41

标签: c# asp.net xml

我正在尝试生成一个类似于:

的XML文档
    <?xml version="1.0" encoding="UTF-8"?>
<Device Version="1.0">
    <Packages>
            <Package Manuf="TI" ModelName="123" MfgPartNumber="CSD86360" Description="DEVICE_INFO" >
                  ".PartialCkt 123 ExtNode = 1 3 4 6 
                  V1 1 3 0
                  V2 4 6 0
                  .EndPartialCkt"
             </Package>
   </Packages>


   <Thermal>
        Manuf="TI" ModelName="123" MfgPartNumber="CSD86360" Description="DEVICE_INFO"
        PowerDissipation="0.1W"
        Material="2-Resistor CTM"
        Thickness="1mm"
        Theta_JB="1.5C/W"
        Theta_JC="0C/W"
        MaxDieTemperature="100C""
   </Thermal>

</Device>

我正在尝试以下方式:

  XmlDocument xmlDoc = new XmlDocument();

 XmlNode DeviceNode= xmlDoc.CreateElement("Device");
                    XmlNode PackageNode= xmlDoc.CreateElement("Packages");
                    PackageNode.AppendChild(Package);
                    ResistorsNode.AppendChild(ResistorNode);
                    XmlAttribute Attr1= xmlDoc.CreateAttribute("ModelName");
                    Attr1.Value = "123";

                    XmlAttribute Attr2 = xmlDoc.CreateAttribute("MfgPartNumber");
                    Attr2.Value =  "CSD86360";

等等..

现在我不确定如何包含普通文本

.PartialCkt 123 ExtNode = 1 3 4 6 
                  V1 1 3 0
                  V2 4 6 0
                  .EndPartialCkt

本文档中不是XML节点。

2 个答案:

答案 0 :(得分:0)

将其设置为节点的Value

XmlNode PackageNode= xmlDoc.CreateElement("Packages");
XmlNode Package = xmlDoc.CreateElement("Package");
Package.Value = ".PartialCkt 123 ExtNode = 1 3 4 6\nV1 1 3 0\nV2 4 6 0\n.EndPartialCkt";
PackageNode.AppendChild(Package);

答案 1 :(得分:0)

使用xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlID = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Device Version=\"1.0\"></Device>";

            XDocument doc = XDocument.Parse(xmlID);
            XElement device = doc.Root;

            XElement packages = new XElement("Packages", new object[] {
                new XElement("Package", new object[] {
                    new XAttribute("Manuf","TI"),
                    new XAttribute("ModelName","123"),
                    new XAttribute("MfgPartNumber","CSD86360"),
                    new XAttribute("Description","DEVICE_INFO"),
                    ".PartialCkt 123 ExtNode = 1 3 4 6\n" +
                    "V1 1 3 0\n" +
                    "V2 4 6 0 .EndPartialCkt"
                })
            });

            device.Add(packages);

            XElement thermal = new XElement("Thermal", new object[] {
                new XAttribute("Manuf","TI"),
                new XAttribute("ModelName","123"),
                new XAttribute("MfgPartNumber","CSD86360"),
                new XAttribute("Description","DEVICE_INFO"),
                new XAttribute("PowerDissipation","0.1W"),
                new XAttribute("Material","2-Resistor CTM"),
                new XAttribute("Thickness","1mm"),
                new XAttribute("Theta_JB","1.5C/W"),
                new XAttribute("Theta_JC","0C/W"),
                new XAttribute("MaxDieTemperature","100C")
            });

            device.Add(thermal);
        }
    }
}