Base operation about XMl file

前端之家收集整理的这篇文章主要介绍了Base operation about XMl file前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

This blog will continue The steps of migrating metadata from SP site 2010 to SP site 2013,and the below is about Base operation about XMl file

Here is the base operation about create xml file:

public class MetadataxML
    {
        public void addWebTag(string xmlFilePath,string webUrl)
        {
            int flagWeb = 0;
            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(xmlFilePath);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList webNodes = rootNode.ChildNodes;
            try
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        flagWeb = 1;
                    }
                }
            }
            catch { }
            if (flagWeb == 0)
            {
                XmlElement webElement = myXmlDoc.CreateElement("Web");
                webElement.SetAttribute("Url",webUrl);
                rootNode.AppendChild(webElement);
                myXmlDoc.Save(xmlFilePath);
            }
        }

        public void addDocumentTag(string xmlFilePath,string webUrl,string documentName,string Metadataname)
        {
            int flagDocument = 0;
            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(xmlFilePath);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList webNodes = rootNode.ChildNodes;
            try
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        XmlNodeList documentNodes = webNode.ChildNodes;
                        foreach (XmlNode documentNode in documentNodes)
                        {
                            if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(Metadataname))
                            {
                                flagDocument = 1;
                            }
                        }
                    }
                }
            }
            catch { }
            if (flagDocument == 0)
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        XmlElement documentElement = myXmlDoc.CreateElement("Document");
                        documentElement.SetAttribute("Name",documentName);
                        documentElement.SetAttribute("Metadata",Metadataname);
                        webNode.AppendChild(documentElement);
                        myXmlDoc.Save(xmlFilePath);
                    }
                }
            }
        }

        public void addFolderTag(string xmlFilePath,String documentName,string Metadataname,string relativeUrl)
        {
            int flagFolder = 0;
            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(xmlFilePath);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList webNodes = rootNode.ChildNodes;
            try
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        XmlNodeList documentNodes = webNode.ChildNodes;
                        foreach (XmlNode documentNode in documentNodes)
                        {
                            if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(Metadataname))
                            {
                                XmlNodeList folderNodes = documentNode.ChildNodes;
                                foreach (XmlNode folderNode in folderNodes)
                                {
                                    if (folderNode.Attributes["RelativeUrl"].Value.Equals(relativeUrl))
                                    {
                                        flagFolder = 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch { }
            if (flagFolder == 0)
            {
                foreach (XmlNode webNode in webNodes)
                {
                    if (webNode.Attributes["Url"].Value.Equals(webUrl))
                    {
                        XmlNodeList documentNodes = webNode.ChildNodes;
                        foreach (XmlNode documentNode in documentNodes)
                        {
                            if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(Metadataname))
                            {
                                XmlElement folderElement = myXmlDoc.CreateElement("Folder");
                                folderElement.SetAttribute("RelativeUrl",relativeUrl);
                                documentNode.AppendChild(folderElement);
                                myXmlDoc.Save(xmlFilePath);
                            }
                        }
                    }
                }
            }
        }

        public void addDocument(string xmlFilePath,string relativeUrl,string fileName,string Metadata)
        {
            addWebTag(xmlFilePath,webUrl);
            addDocumentTag(xmlFilePath,webUrl,documentName,Metadataname);
            addFolderTag(xmlFilePath,Metadataname,relativeUrl);

            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(xmlFilePath);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
            XmlNodeList webNodes = rootNode.ChildNodes;
            foreach (XmlNode webNode in firstLevelNodeList)
            {
                if (webNode.Attributes["Url"].Value.Equals(webUrl))
                {
                    XmlNodeList documentNodes = webNode.ChildNodes;
                    foreach (XmlNode documentNode in documentNodes)
                    {
                        if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(Metadataname))
                        {
                            XmlNodeList folderNodes = documentNode.ChildNodes;
                            foreach (XmlNode folderNode in folderNodes)
                            {
                                if (folderNode.Attributes["RelativeUrl"].Value.Equals(relativeUrl))
                                {
                                    XmlElement newElement = myXmlDoc.CreateElement("Item");
                                    newElement.SetAttribute("Name",fileName);
                                    newElement.InnerText = Metadata;
                                    folderNode.AppendChild(newElement);
                                }
                            }
                        }
                    }
                }
            }
            myXmlDoc.Save(xmlFilePath);
        }

        public List<string> getDocument(XmlDocument myXmlDoc,string fileName)
        {
            List<string> Metadata = new List<string>();

            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList firstLevelNodeList = rootNode.ChildNodes;

            foreach (XmlNode webNode in firstLevelNodeList)
            {
                if (webNode.Attributes["Url"].Value.Equals(webUrl))
                {
                    XmlNodeList documentNodes = webNode.ChildNodes;
                    foreach (XmlNode documentNode in documentNodes)
                    {
                        if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(Metadataname))
                        {
                            XmlNodeList folderNodes = documentNode.ChildNodes;
                            foreach (XmlNode folderNode in folderNodes)
                            {
                                if (folderNode.Attributes["RelativeUrl"].Value.Equals(relativeUrl))
                                {
                                    XmlNodeList itemsNode = folderNode.ChildNodes;
                                    foreach (XmlNode itemNode in itemsNode)
                                    {
                                        if (itemNode.Attributes["Name"].Value.Equals(fileName))
                                        {
                                            Metadata.Add(itemNode.InnerText);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return Metadata;
        }

        public void DeleteDocument(string path,string Metadataname)
        {
            XmlDocument myXmlDoc = new XmlDocument();
            myXmlDoc.Load(path);
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Site");
            XmlNodeList firstLevelNodeList = rootNode.ChildNodes;

            foreach (XmlNode webNode in firstLevelNodeList)
            {
                if (webNode.Attributes["Url"].Value.Equals(webUrl))
                {
                    XmlNodeList documentNodes = webNode.ChildNodes;
                    foreach (XmlNode documentNode in documentNodes)
                    {
                        if (documentNode.Attributes["Name"].Value.Equals(documentName) && documentNode.Attributes["Metadata"].Value.Equals(Metadataname))
                        {
                            documentNode.RemoveAll();
                            webNode.RemoveChild(documentNode);
                            myXmlDoc.Save(path);
                            return;
                        }
                    }
                }
            }
        }

        private string importXmlFile()
        {
            string xmlPath = "";
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            dlg.Filter = "(文本文件*.xml)|*.xml";
            dlg.FilterIndex = 1;
            dlg.RestoreDirectory = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    xmlPath = dlg.FileName;
                }
                catch (Exception)
                {
                    MessageBox.Show("testdata is wrong");
                }
            }
            return xmlPath;
        }

        private void exportXmlFile()
        {
            string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "SaveData";               //设置标题
            sfd.AddExtension = true;               //是否自动增加所辍名
            sfd.FileName = "storeportal.xml";
            sfd.InitialDirectory = str;  //定义打开的默认文件夹位置
            sfd.Filter = "Text.File(*.xml)|*.xml";
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    string myXMLFilePath = "C:\\Users\\v-trdong\\Desktop\\MyComputers.xml";
                    MetadataxML xml = new MetadataxML();
                    XmlDocument myXmlDoc = new XmlDocument();
                    myXmlDoc.Load(myXMLFilePath);
                    string fileName = sfd.FileName;
                    myXmlDoc.Save(fileName);
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.Message,"Simple Editor",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                }
            }
        }
    }







猜你在找的XML相关文章