熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

在.NET中操作XmlDocument

2022-06-13   來源: ASP編程 

  大家想必一定都了解XML利用XML技術來存儲數據和文檔是一件很容易的事情NET Framework 在它的命名空間SystemXml 就提供了一種可以很方便的操作xml的類XmlDocument它使用起來非常容易XmlDocument 其實就是一個簡單的樹下面詳細的介紹XmlDocument 的使用方法

  下面是這個類中操作節點的常用方法

  // create a new node in the document object from the source node
    //and name it as sName
// the return value indicates success or failure
public bool AddNode(XmlNode oSource String sName);

  // same as above except that it also specifies the parent node of the
    // newly created node
// the return value indicates success or failure (returns false if the
    // parent node does not exist)
public bool AddNode(XmlNode oSource String sName String sParent);

  // create a set of new nodes in the document object from the source node
    // list and name them as sName
// the return value indicates success or failure
public bool AddNodes(XmlNodeList oSourceList String sName);

  // same as above except that it also specifies the parent node of the
    // newly created nodes the return value indicates success or failure
    // (returns false if the parent node
    // does not exist)
public bool AddNodes(XmlNodeList oSourceList String sName String sParent);

  // merge the source node into a node named sName in the document object
// the node named sName will be created if it does not exist
// the return value indicates success or failure
public bool MergeNode(XmlNode oSource String sName);

  // same as above except that it also specifies the parent node of the merged node
// the return value indicates success or failure (returns false if the parent node
    // does not exist)
public bool MergeNode(XmlNode oSource String sName String sParent);

  下面我們給一個增加節點的例子

  docVechilexml

  <VehicleData>
    <Record>
        <id></id>
        <make>Ford</make>
        <model>Escort</model>
        <year></year>
    </Record>
    <Record>
        <id></id>
        <make>Toyota</make>
        <model>Tercel</model>
        <year></year>
    </Record>
    <Record>
        <id></id>
        <make>Mazda</make>
        <model>GLC</model>
        <year></year>
    </Record>
</VehicleData>
docDriverxml

  <DriverData>
    <Record>
        <id></id>
        <firstname>Albert</firstname>
        <lastname>Einstein</lastname>
    </Record>
    <Record>
        <id></id>
        <firstname>Clint</firstname>
        <lastname>Eastwood</lastname>
    </Record>
    <Record>
        <id></id>
        <firstname>James</firstname>
        <lastname>Bond</lastname>
    </Record>
</DriverData>

  下面的代碼將增加一個節點

  Dim myDoc As XMLDocumentEx = New XMLDocumentEx()
myDocLoadXml(<Data></Data>)
myDocAddNode(docVehicleSelectSingleNode(//Record) VehicleRecord Data)
myDocAddNode(docDriverSelectSingleNode(//Record) DriverRecord Data)
myDocxml<Data>
    <VehicleRecord>
         <id></id>
        <make></make>
        <model></model>
        <year></year>
    </ Vehicle Record>
    <DriverRecord>
        <id></id>
        <firstname></firstname>
        <lastname></lastname>
    </DriverRecord>
   
</Data> 我們也可是使用AddNodes方法把一個記錄集的所有記錄增加到節點上Dim myDoc As XMLDocumentEx = New XMLDocumentEx()
myDocLoadXml(<Data> <VehicleData></Vehicle Data><DriverData></DriverData> </Data>)
myDocAddNodes(docVehicleSelectNodes(//Record) VehicleRecord Vehicle Data)
myDocAddNodes(docDriverSelectNodes(//Record) DriverRecord DriverData)結果如下myDocxml<Data>
     <VehicleData>
        <VehicleRecord>
            <id></id>
            <make>Ford</make>
            <model>Escort</model>
            <year></year>
        </VehicleRecord>
        <VehicleRecord>
            <id></id>
            <make>Toyota</make>
            <model>Tercel</model>
            <year></year>
        </VehicleRecord>
        <VehicleRecord>
            <id></id>
            <make>Mazda</make>
            <model>GLC</model>
            <year></year>
        </VehicleRecord>
    </VehicleData>
     <DriverData>
        <DriverRecord>
            <id></id>
            <firstname>Albert</firstname>
            <lastname>Einstein</lastname>
        </DriverRecord>
        <DriverRecord>
            <id></id>
            <firstname>Clint</firstname>
            <lastname>Eastwood</lastname>
        </DriverRecord>
        <DriverRecord>
            <id></id>
            <firstname>James</firstname>
            <lastname>Bond</lastname>
        </DriverRecord>
    </DriverData>
   
</Data>下面我介紹如何合並節點假設我們有兩個XmlDocument文件docBook和docBook這兩個文檔都包含 <Book> 節點  在docBook 中的這個 <Book> 節點 包含 <Introduction> <Chapter> and <Chapter>  在docBook中的這個 <Book> 節點 包含 <Chapter> <Chapter> and <Chapter>  下面的代碼演示如何合並這兩個book節點:Dim myDoc As XMLDocumentEx = New XMLDocumentEx()
myDocLoadXml(<Data> <Book></Book></Data> )
myDocMergeNode(docBookSelectSingleNode(//Book) Book Data )
myDocMergeNode(docBookSelectSingleNode(//Book) Book Data)合並後的效果如下myDocxml<Data>
    <Book>
        <Introduction></Introduction>
        < Chapter ></Chapter>
        <Chapter></Chapter>
        <Chapter></Chapter>
        <Chapter></Chapter>
        <Chapter></Chapter>
    </Book>
</Data>下面是所有的源代碼sealed public class XMLDocumentEx: XmlDocument
{
    public bool AddNode(XmlNode oSource String sName)
    {
        return AddNode(oSource sName null);
    }
    public bool AddNode(XmlNode oSource String sName String sParent)
    {
        try
        {
            if(sName!=null&&oSource!= null)
            {
                // create the new node with given name
                XmlNode oNewNode = CreateElement(sName);
                // copy the contents from the source node
                oNewNodeInnerXml = oSourceInnerXml;
                // if there is no parent node specified then add
                // the new node as a child node of the root node
                if(sParent!= null) sParent = sParentTrim();
                if(sParent== null||sParentEquals(StringEmpty))
                {
                    DocumentElementAppendChild(oNewNode);
                    return true;
                }
                // otherwise add the new node as a child of the parent node
                else
                {
                    if (!sParentSubstring()Equals(//)) sParent = //+sParent;
                    XmlNode oParent = SelectSingleNode(sParent);
                    if (oParent!=null)
                    {
                        oParentAppendChild(oNewNode);
                        return true ;
                    }
                }
            }
        }
        catch (Exception)
        {
            // error handling code
        }
        return false;
    }
    public bool AddNodes(XmlNodeList oSourceList String sName)
    {
        return AddNodes(oSourceList sName null);
    }
    public bool AddNodes(XmlNodeList oSourceList String sName String sParent)
    {
        try
        {
            if(oSourceList!= null)
            {
                // call AddNode for each item in the source node list
                // return true only if all nodes are added successfully
                int i = ;
                while(i<oSourceListCount)
                {
                    if (!AddNode(oSourceListItem(i)sNamesParent)) return false;
                    i++;
                }
                return true;
            }
        }
        catch (Exception)
        {
            // error handling code
        }
        return false;
    }
    public bool MergeNode(XmlNode oSource String sName)
    {
        return MergeNode(oSource sName null );
    }
    public bool MergeNode(XmlNode oSource String sName String sParent)
    {
        try
        {
            if(sName!=null&&oSource!= null)
            {
                XmlNode theNode = null ;
                // if there is no parent node specified
                if(sParent!= null) sParent = sParentTrim();
                if(sParent== null||sParentEquals(StringEmpty))
                {
                    // if the node with specified name does not exist
                    // add it as a child node of the root node
                    theNode = SelectSingleNode(//+sName);
                    if (theNode==null)
                    {
                        theNode = CreateElement(sName);
                        DocumentElementAppendChild(theNode);
                    }
                }
                // if the parent node is specified
                else
                {
                    // find the parent node
                    if (!sParentSubstring()Equals(//)) sParent = //+sParent;
                    XmlNode theParent = SelectSingleNode(sParent);
                    if (theParent!=null)
                    {
                        // if the node with specified name does not exist create
                        // it first then add it as a child node of the parent node
                        theNode = theParentSelectSingleNode(sName);
                        if(theNode==null)
                        {
                            theNode = CreateElement(sName);
                            theParentAppendChild(theNode);
                        }
                    }
                }
                // merge the content of the source node into
                // the node with specified name
                if(theNode!= null)
                {
                    theNodeInnerXml += oSourceInnerXml;
                    return true;
                }
            }
        }
        catch (Exception)
        {
        }
        return false;
    }
}


From:http://tw.wingwit.com/Article/program/ASP/201311/21670.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.