简体   繁体   中英

Add plain text without xml elements in XML

I am trying to generate an XML document which looks like:

    <?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>

I am trying in the following way:

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

And so on..

Now I am not sure on how to include the normal text

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

Which is not an XML node in this document.

Set it as the Value of the node.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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