<?xml version="1.0" encoding="utf-8"?>
<Devices>
    <Device>
        <AnalogConfigs>           
            <AnalogConfig>
                <AnalogName>无</AnalogName>
                <AnalogNum>16</AnalogNum>
                <AnalogRate>0.00</AnalogRate>
                <AnalogBase>0.00</AnalogBase>
                <AnalogUpperLimit>0.00</AnalogUpperLimit>
                <AnalogLowerLimit>0.00</AnalogLowerLimit>
                <AnalogHysteresis>0.00</AnalogHysteresis>
                <AnalogDelay>0.00</AnalogDelay>
            </AnalogConfig>
        </AnalogConfigs>
        <SwitchConfigs>
            <SwitchConfig>
                <SwitchName>无</SwitchName>
                <SwitchNum>2</SwitchNum>
                <SwitchStatus>关</SwitchStatus>
            </SwitchConfig>
        </SwitchConfigs>
        <UPSInfos>
            <UPSInfo>
                <UpsName>无</UpsName>
                <UpsStyle>无</UpsStyle>
                <UpsState>无</UpsState>
            </UPSInfo>
        </UPSInfos>
    </Device>
</Devices>我现在想在 <UPSInfos>下再加一个 <UPSInfo>信息 这样就变为:
     <UPSInfos>
            <UPSInfo>
                <UpsName>无</UpsName>
                <UpsStyle>无</UpsStyle>
                <UpsState>无</UpsState>
            </UPSInfo>
       <UPSInfo>
                <UpsName>无</UpsName>
                <UpsStyle>无</UpsStyle>
                <UpsState>无</UpsState>
            </UPSInfo>
        </UPSInfos>
我应该怎么做呢?
请高手帮忙啊,在线等待

解决方案 »

  1.   

    参考一下http://blog.csdn.net/lovefootball/archive/2008/08/21/2785922.aspx
    XmlDocument doc = new XmlDocument(); 
    doc.Load(你的文件);
    XmlNode upsInfos = doc.SelectSingleNode("/Devices/Device/UPSInfos");
    XmlElement upsInfo = doc.CreateElement("UPSInfo"); 
    XmlElement upsName = doc.CreateElement("UpsName");
    upsName.InnerText = "无";
    XmlElement upsStyle = doc.CreateElement("UpsStyle"); 
    upsStyle.InnerText = "无";
    XmlElement upsState = doc.CreateElement("UpsState"); 
    upsState.InnerText = "无";
    upsInfo.AppendChild(upsName); 
    upsInfo.AppendChild(upsStyle); 
    upsInfo.AppendChild(upsState); 
    upsInfos.AppendChild(upsInfo); 
    doc.Save(你的文件);
      

  2.   

    tring sFileName = "ExpXml.xml";
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(sFileName);
    XmlNode node = xmldoc.DocumentElement.ChildNodes[0].CloneNode(true);
    node.Attributes["ID"].Value = "2";
    node.Attributes["name"].Value = "x-y";
    node["Param"].Attributes["name"].Value = "x";
    node["Param"].InnerText="aaaa";
    //......
    xmldoc.DocumentElement.AppendChild(node);
    xmldoc.Save(sFileName);
    <?xml version="1.0" encoding="utf-8" ?> <Expressions> <Expression ID="1" name="a+b" re="测试一">
    <Param name="a" re="注解1"></Param>
    <Param name="b" re="注解2"></Param>
    </Expression>
             <Expression ID="2" name="x-y" re="测试二">
    <Param name="x" re="注解1"></Param>
    <Param name="y" re="注解2"></Param>
    </Expression>
    </Expressions>
    看着该
      

  3.   

    参考
    http://www.cnblogs.com/jackie-hy/archive/2008/01/12/1035888.html
    http://www.cnblogs.com/David-weihw/archive/2008/11/28/656327.html
    http://www.cnblogs.com/dotnetbbs/articles/449524.html
      

  4.   


    //要加上using System.Xml.Linq;
    XDocument doc = XDocument.Load(@"C:\test.xml");
    XElement ele = doc.Root;
    XElement df = new XElement("UpsName", "无");XElement ups = new XElement("UpsInfo");
    ups.Add(df);
    df = new XElement("UpsStyle", "无");
    ups.Add(df);
    df = new XElement("UpsState", "无");
    ups.Add(df);ele = ele.Element("Device").Element("UPSInfos");
    ele.Add(ups);
    doc.Save(@"C:\test.xml");
      

  5.   

    为什么无法添加using System.Xml.Linq啊
      

  6.   

    我用的是ASP.NET 2.0,好象不支持using System.Xml.Linq
      

  7.   

    这样的话就只能用System.Xml中的方法了