C#中初学XML,有点晕,有没有什么方法可以通用的操作xml,读写查询替换等。而不管xml的格式是什么样(有命名空间或没有的)?
在操作xml还有什么readxml和loadxml方式,有些节点有值,有些节点空白,搞了几天还是不清楚。
希望有人做个汇总,对xml的操作,对于任意格式的xml都可以进行读写,任意节点查询等。
不知道行不行?菜鸟期待中.................
在操作xml还有什么readxml和loadxml方式,有些节点有值,有些节点空白,搞了几天还是不清楚。
希望有人做个汇总,对xml的操作,对于任意格式的xml都可以进行读写,任意节点查询等。
不知道行不行?菜鸟期待中.................
xmlDoc.load(filePath);
XmlNode root = xmlDoc.DocumentElement;string xpath = "xpath查询条件";
//查询多个节点
XmlNodeList xnList = root.SelectNodes(xpath);
//查询单个节点
XmlNOde node = root.SelectSingleNode(xpath);===============================================
我是这么用的,从来不管名称空间。Doc对象的载入可以多种方式,路径,文件流,ReadXml对象等等。
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;
using System.Management;
using System.Net;
namespace Zhzuo
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string strXml="<?xml version=\"1.0\"?>"
+"<Data>"
+"<Head>"
+"<Nodeid>1111</Nodeid>"
+"<Subid>2222</Subid>"
+"<Version>2004</Version>"
+"<Date>20040302</Date>"
+"<Time>101500</Time>"
+"</Head>"
+"<Body>"
+"<Code>01</Code>"
+"<Name>深圳</Name>"
+"<IdType>0</IdType>"
+"<Idno>110258740824082</Idno>"
+"</Body>"
+"</Data>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
string vv;
vv = doc.GetElementsByTagName("Version")[0].InnerText;
Console.WriteLine(vv);
vv = doc.SelectNodes("//Version")[0].InnerText;
Console.WriteLine(vv);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using System.Xml.Xsl;
using System.Xml;
using System.Configuration;
using System.Data;
using Microsoft.VisualBasic;/*------------------------------------------------
http://blog.csdn.net/zhzuo
--------------------------------------------------*/
namespace Zhzuo.VS2005Test.ConsoleTest
{
class Program
{
private static DateTime ds;
private static int i;
private static bool n; static void Main(string[] args)
{
string strXml = "<?xml version=\"1.0\"?>"
+ "<items>"
+ "<item>"
+"<id>5</id>"
+ "<name>0.15</name>"
+ "</item>"
+ "<item>"
+"<id>6</id>"
+ "<name>0.20</name>"
+ "</item>"
+ "<item>"
+"<id>7</id>"
+ "<name>0.30</name>"
+ "</item>"
+ "<item>"
+"<id>8</id>"
+ "<name>0.40</name>"
+ "</item>"
+ "<item>"
+"<id>9</id>"
+ "<name>0.50</name>"
+ "</item>"
+ "</items>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
XmlNodeList nodeList = doc.SelectNodes("//item[id>7]");
Console.WriteLine(nodeList.Count); Console.ReadLine();
}
}
}
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;
using System.Management;
using System.Net;
namespace Zhzuo
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string strXml="<?xml version=\"1.0\"?>"
+"<Data>"
+"<Head>"
+"<Nodeid>1111</Nodeid>"
+"<Subid>2222</Subid>"
+"<Version>2004</Version>"
+"<Date>20040302</Date>"
+"<Time>101500</Time>"
+"</Head>"
+"<Body>"
+"<Code>01</Code>"
+"<Name>深圳</Name>"
+"<IdType>0</IdType>"
+"<Idno>110258740824082</Idno>"
+"</Body>"
+"</Data>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
string vv;
XmlNodeList myNodes = doc.GetElementsByTagName("Version");
vv = myNodes[0].InnerText;
//修改
myNodes[0].InnerText = "123455";
Console.WriteLine(vv);
myNodes = doc.SelectNodes("//Version");
vv = myNodes[0].InnerText;
Console.WriteLine(vv);
//修改
myNodes[0].InnerText = "67890";
doc.Save("d:\\text.xml");
Console.ReadLine();
}
}
}
//<?xml version="1.0" encoding="utf-8" ?>
/*
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>"); //Create an XML declaration.
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0",null,null); //Add the new node to the document.
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out); */
if (doc.FirstChild.NodeType != XmlNodeType.XmlDeclaration)
{
XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
}
XmlDocument xdoc = new XmlDocument();
xdoc.Load("input.xml");
List<XmlNode> nodesToRemove=new List<XmlNode>();
foreach (XmlNode item in xdoc.DocumentElement.SelectNodes("item"))
{
if (item.SelectNodes("section").Count == 2)
{
nodesToRemove.Add(item);
}
}
foreach (XmlNode section in nodesToRemove)
{
xdoc.DocumentElement.RemoveChild(section);
}
xdoc.Save(@"..\..\output.xml");
Console.WriteLine(XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Local));//给电子口岸公司的报文
public void RELEntryMessage()
{
string[] inFiles = Directory.GetFiles(RELDECEntryIn, "*.xml", SearchOption.TopDirectoryOnly);
if (inFiles.Length == 0)
{
return;
} int current = 100;
if (inFiles.Length > MaxMessageCount)
{
current = MaxMessageCount;
}
else
{
current = inFiles.Length;
}
for (int i = 0; i < current; i++)
{
try
{
XmlDocument doc = new XmlDocument();
//string filename = Path.Combine(DECEntryIn, inFiles[i]);
doc.Load(inFiles[i]);
SetDeclaration(doc);
if (doc.SelectSingleNode("/NewDataSet/ENTRY_HEAD") != null && doc.SelectSingleNode("/NewDataSet/ENTRY_WORKFLOW[STEP_ID='70000000']") != null)
{
//string tempFileName = Path.Combine(RELDECEntryTemp, Path.GetFileName(inFiles[i]));
string newFileName = Path.Combine(RELDECEntryOut, Path.GetFileName(inFiles[i]));//
Stream ms = new FileStream(newFileName, FileMode.Create, FileAccess.Write);//MemoryStream();
using (XmlTextWriter w = new XmlTextWriter(ms, Encoding.UTF8))
{
//XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
w.WriteStartDocument();
//w.WriteNode(xmldecl.CreateNavigator(), true);
relMessageTransform.Transform(doc, w);//new XsltArgumentList(),ms);
w.Close();
}
////加
//XmlDocument doc1 = new XmlDocument();
//doc1.Load(tempFileName);
//SetDeclaration(doc1); //doc1.Save(newFileName);
}
File.Delete(inFiles[i]);
}
catch (Exception ex)
{
EventLog.WriteEntry("BizTalk Debug", "转换电子口岸放行报文失败:" + inFiles[i] + ex.ToString());
} }
} private void SetDeclaration(XmlDocument doc)
{
if (doc.FirstChild.NodeType != XmlNodeType.XmlDeclaration)
{
XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
}
}
//doc.Load("C:\\nso.xml");
doc.Load("C:\\nso.xml");
NameTable xmt = new NameTable();
//xmt.Add("ns0");
XmlNamespaceManager xnm = new XmlNamespaceManager(xmt);
xnm.AddNamespace("ns0", "http://schemas.nbeport.gov.cn/H2KSubscription/DEC");
XmlNode node = doc.SelectSingleNode("//ns0:NewDataSet/ns0:ENTRY_WORKFLOW", xnm);
XmlNode node = doc1.SelectSingleNode("/NewDataSet/ENTRY_WORKFLOW[STEP_ID='70000000']/CREATE_DATE/text()");
string ser = node.Value;//如果是默认命名空间,可以添加别名进行查询。
XmlDocument doc = new XmlDocument();
//doc.Load("C:\\nso.xml");
doc.Load("C:\\m.xml");
NameTable xmt = new NameTable();
XmlNamespaceManager xnm = new XmlNamespaceManager(xmt);
//默认命名空间添加nn别名。
xnm.AddNamespace("nn", "http://schemas.nbeport.gov.cn/H2KSubscription/DEC");
XmlNode node = doc.SelectSingleNode("//nn:NewDataSet/nn:ENTRY_WORKFLOW", xnm);