C#中用XMLDocument写文件时,去掉XMLNS属性

原文:http://blog.csdn.net/honkerhero/article/details/1675824
核心:在CreateElement时设置  newDoc.CreateElement("orderinfo", newDoc.DocumentElement.NamespaceURI);

 

private Boolean CreateNewEcg(string oldEcgXML, string newEcgXML)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(oldEcgXML);

                XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
                ns.AddNamespace("", "http://www3.medical.philips.com");
                ns.AddNamespace("default", "http://www3.medical.philips.com");

                XmlNode xnDocumentinfo = doc.SelectSingleNode("descendant::default:documentinfo", ns);
                XmlNode xnPatient = doc.SelectSingleNode("descendant::default:patient", ns);
                XmlNode xnOrder = doc.SelectSingleNode("descendant::default:orderinfo", ns);
                XmlNode interpretations = doc.SelectSingleNode("descendant::default:interpretation", ns);
                List<string> statementList = new List<string>();
                //interpretation
                if (interpretations != null)
                {
                    foreach (XmlNode xnState in interpretations.SelectNodes("descendant::default:statement", ns))
                    {
                        statementList.Add(xnState.InnerText);
                    }
                } 

                XmlDocument newDoc = new XmlDocument();
                XmlDeclaration dec = newDoc.CreateXmlDeclaration("1.0", "utf-16", null);
                newDoc.AppendChild(dec);
                //create root 
                XmlElement xmlRoot = newDoc.CreateElement("", "restingecgdata", "http://www3.medical.philips.com");//"http://www3.medical.philips.com"
                newDoc.AppendChild(xmlRoot);
                //add documentinfo 
                xmlRoot.AppendChild(newDoc.ImportNode(xnDocumentinfo,true));
                
                //add patient
                if (xnPatient != null)
                {
                    xmlRoot.AppendChild(newDoc.ImportNode(xnPatient, true));
                }
                else
                {
                    xmlRoot.AppendChild(newDoc.CreateElement("patient", newDoc.DocumentElement.NamespaceURI));
                }
                //add order 
                if (xnOrder != null)
                {
                    xmlRoot.AppendChild(newDoc.ImportNode(xnOrder,true));
                }
                else
                {
                    xmlRoot.AppendChild(newDoc.CreateElement("orderinfo", newDoc.DocumentElement.NamespaceURI));
                }
                //add statements
                if (statementList.Count > 0)
                {
                    XmlNode nodeStates = newDoc.CreateElement("statements", newDoc.DocumentElement.NamespaceURI);//newDoc.CreateElement("","statements","");
                    XmlElement element;
                    foreach (string str in statementList)
                    {
                        element = newDoc.CreateElement("statement", newDoc.DocumentElement.NamespaceURI);
                        element.InnerText = str;
                        nodeStates.AppendChild(element);
                    }
                    xmlRoot.AppendChild(nodeStates);
                }
                else
                {
                    xmlRoot.AppendChild(newDoc.CreateElement("statements", newDoc.DocumentElement.NamespaceURI));//,newDoc.DocumentElement.NamespaceURI
                }  
                newDoc.Save(newEcgXML);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
<?xml version="1.0" encoding="utf-16"?>
<restingecgdata xmlns="http://www3.medical.philips.com">
  <documentinfo>
    <documentname>60857a22-afb6-4141-b207-4b432aac67a7.xml</documentname>
    <documenttype>PhilipsECG</documenttype>
    <documentversion>1.04</documentversion>
    <editor date="2014-07-30" time="07:12:40" id="S-1-5-21-1850833516-2623192688-2068026971-500">Administrator</editor>
    <comments>XML document modified by TraceMasterVue's ECGTranslator (for XML to XML conversions) on Fri Oct 05 12:38:53 2007</comments>
    <comments>DXL Analysis version PH110C, 2014-6-27 performed on 2014-07-30 07:12:31</comments>
    <documenttimestamp>0x3F243</documenttimestamp>
  </documentinfo>
  <patient criteriaversionforpatientdata="0C">
    <generalpatientdata>
      <patientid>$$$7168</patientid>
      <name>
        <lastname>WEBB</lastname>
        <firstname>JOSEPHINE</firstname>
      </name>
      <age>
        <dateofbirth>1936-07-02</dateofbirth>
      </age>
      <pacestatus>Unknown</pacestatus>
      <sex>Female</sex>
    </generalpatientdata>
  </patient>
  <orderinfo />
  <statements>
    <statement>RALARVRight and left arm electrode reversal, interpretation assumes no reversal</statement>
    <statement>JTJunctional tachycardiaabsent P waves, rapid V-rate</statement>
    <statement>VBIGVentricular bigeminybigeminy string&gt;4 w/ V complexes</statement>
    <statement>RBBBRight bundle branch blockQRSd&gt;120, terminal axis(90,270)</statement>
    <statement>LVOLFLow voltage, extremity leadsall extremity leads &lt;0.5mV</statement>
    <statement>SD2WIST depression, consider ischemia, diffuse ldsST &lt;-0.10mV, ant/lat/inf</statement>
    <statement>CINJLST elevation, consider lateral injuryST &gt;0.10mV, I aVL V5 V6</statement>
  </statements>
</restingecgdata>


 

posted @ 2014-08-04 10:04  振乾  阅读(938)  评论(0)    收藏  举报