.NET Language-Integrated Query for XML Data
XNamespace myNs = "http://mycompany.com";XElement contacts = new XElement(myNs + "contacts", new XElement(myNs + "contact", new XElement(myNs + "name", "Patrick Hines"), new XElement(myNs + "phone", "206-555-0144", new XAttribute("type", "home")), new XElement(myNs + "phone", "425-555-0145", new XAttribute("type", "work")), new XElement(myNs + "address", new XElement(myNs + "street1", "123 Main St"), new XElement(myNs + "city", "Mercer Island"), new XElement(myNs + "state", "WA"), new XElement(myNs + "postal", "68042") ) ) );
产生如下文件:在最上层指定命名空间,分节点默认还有父节点的命名空间*继承性)
Patrick Hines 206-555-0144 425-555-0145 123 Main St Mercer Island WA 68042
XML Namespaces
Code Project:
private Dictionary, KeyValuePair > RetriveDataFromXmLFile(string xmlFilePath, out KeyValuePair guidComment, out KeyValuePair guidDescription) { // XML format // // Dictionary// //// //EvaluationRule //Update //1055 //// //1055 //AddPI_ACHCHECK_USs //TestByBob3 //TestByBob3 //86b6d584-2fb9-45fe-aea7-acc4479a3b3f //, KeyValuePair > groupsChange = new Dictionary , KeyValuePair >(); XNamespace urnl = "urn:schemas.amc.com/Cdefce/Name/Mode/2011/04"; XNamespace i = "http://www.w3.org/2001/XMLSchema instance"; XElement elem = XElement.Load(xmlFilePath); string changeGuid = elem.Element(urnl + "GroupId").Value; string comments = elem.Element(urnl + "Comments").Value; string description = elem.Element(urnl + "Description").Value; guidComment = new KeyValuePair (changeGuid, comments); guidDescription = new KeyValuePair (changeGuid, description); var configChanges = elem.Elements(urnl + "Changes").Elements(urnl + "ConfigChange"); foreach (var change in configChanges) { string key = change.Element(urnl + "Key").Value; string configObjectType = change.Element(urnl + "ConfigObjectType").Value; string text = change.ToString(); KeyValuePair idXml = new KeyValuePair (changeGuid, text); KeyValuePair typeKey = new KeyValuePair (configObjectType, key); groupsChange.Add(typeKey, idXml); } return groupsChange; }