金沙棋牌app手机下载

当前位置:金沙棋牌 > 金沙棋牌app手机下载 > 中文汉字

中文汉字

来源:http://www.logblo.com 作者:金沙棋牌 时间:2019-11-08 07:59

正文转自:

In the previous chapter, we wrote XML using the XmlWriter class. However, for some situations, especially when doing updates of existing XML, using the XmlDocument class can come in handy. You should be aware of the higher memory consumption though, mainly for large XML documents. Here is some code:

According to the Scripting.FileSystemObject documentation.aspx), the CreateTextFile.aspx) method takes a Boolean value to create a Unicode file. You could change the T-SQL code to use that method instead of OpenTextFile.

using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace WritingXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode rootNode = xmlDoc.CreateElement("users");
            xmlDoc.AppendChild(rootNode);

            XmlNode userNode = xmlDoc.CreateElement("user");
            XmlAttribute attribute = xmlDoc.CreateAttribute("age");
            attribute.Value = "42";
            userNode.Attributes.Append(attribute);
            userNode.InnerText = "John Doe";
            rootNode.AppendChild(userNode);

            userNode = xmlDoc.CreateElement("user");
            attribute = xmlDoc.CreateAttribute("age");
            attribute.Value = "39";
            userNode.Attributes.Append(attribute);
            userNode.InnerText = "Jane Doe";
            rootNode.AppendChild(userNode);

            xmlDoc.Save("test-doc.xml");
        }
    }
}

EDIT:

And the resulting XML:

Below is an example using OpenTextFile per Tom's comment. I change the iomode value in your original code from 1 to 8 to match the documentation and added the close and destroy in case your full code is missing those important tasks.

<users>
  <user age="42">John Doe</user>
  <user age="39">Jane Doe</user>
</users>
DECLARE @OLE INT;
DECLARE @FileId INT;
DECLARE @File VARCHAR(max) = 'c:test.txt';
DECLARE @Text NVARCHAR(max) = N'من نمایش داده نمیشم';

EXECUTE sp_OACreate 'Scripting.FileSystemObject',@OLE OUT;
EXECUTE sp_OAMethod @OLE, 'OpenTextFile', @FileId OUT, @File,8,1,-1;
EXECUTE sp_OAMethod @FileId, 'WriteLine', Null, @Text;
EXECUTE sp_OAMethod @FileId, 'Close';
EXECUTE sp_OADestroy @FileId OUT;
EXECUTE sp_OADestroy @OLE OUT;

As you can see, this is a bit more object oriented than the XmlWriter approach, and it does require a bit more code. What we do is that we instantiate an XmlDocument object, which we will use to create both new elements and attributes, using the CreateElement() and CreateAttribute() methods. Each time we do that, we append the elements, either directly to the document, or to another element. You can see this in the example, where the root element ("users") is appended directly to the document, while the user elements are appended to the root element. Attributes are of course appended to the elements where they belong, using the Append() method on the Attributes property. The entire XML document is written to the disk on the last line, where we use the Save() method. 

 

Now, as already mentioned, this requires a bit more code than when using the XmlWriter, but imagine a situation where you just need to go into an existing XML document and change a few values. Using the XmlWriter approach, you would have to first read all the information using an XmlReader, store it, change it, and then write the entire information back using the XmlWriter. Because the XmlDocument holds everything in memory for you, updating an existing XML file becomes a lot simpler. The following example opens the "test-doc.xml" file that we created in the previous chapter and increases every user's age by one. See how easy it is:

using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace WritingXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("test-doc.xml");
            XmlNodeList userNodes = xmlDoc.SelectNodes("//users/user");
            foreach(XmlNode userNode in userNodes)
            {
                int age = int.Parse(userNode.Attributes["age"].Value);
                userNode.Attributes["age"].Value = (age + 1).ToString();
            }
            xmlDoc.Save(@"D:test-doc.xml");           
        }
    }
}

We load the XML file and ask for all the <user> nodes. We then iterate through them, read the age attribute into an integer variable, and then we write the value back to the node and attribute, after increasing the value by 1. At last, we save the document back to the same file and if you open it up, you will see that our users all just had a birthday. Pretty cool!

本文由金沙棋牌发布于金沙棋牌app手机下载,转载请注明出处:中文汉字

关键词:

上一篇:没有了

下一篇:没有了