Вы находитесь на странице: 1из 12

XML C#

XML-
XML
,
.
.NET
XML.
XML-,
, xml-
, c#.
, :
1
2
3
4
5
6

class User
{
public string Name { get; set; }
public int Age { get; set; }
public string Company { get; set; }
}

C# User:
1
2
3

User user1 = new User { Name = "Bill Gates", Age = 48, Company = "Microsoft" };
User user2 = new User { Name = "Larry Page", Age = 42, Company = "Google" };
List<User> users = new List<User> { user1, user2 };

xml
xml-:
1
2
3
4
5
6
7
8

<?xml version="1.0" encoding="utf-8" ?>


<users>
<user name="Bill Gates">
<company>Microsoft</company>
<age>48</age>
</user>
<user name="Larry Page">
<company>Google</company>

9
10
11

<age>48</age>
</user>
</users>

XML- <?xml version="1.0" encoding="utf-8" ?>.


(1.0) (utf-8) xml.
.
XML- ,
.
<users>.
<users> <user>.
user.

, , <user> </user>,
.
: <user /> -
.
.
user company age
name.

: ="". , <user name="Bill Gates">,
name Bill Gates
.
, <company>Google</company> - company
Google.
,
<company> <COMPANY> .
, Users C#
<users>, User - <user>,
User -
<user>

- ?
- ,
. ,
:
1
2
3
4
5
6
7
8
9
10
11
12
13

<?xml version="1.0" encoding="utf-8" ?>


<users>
<user>
<name>Bill Gates</name>
<company>Microsoft</company>
<age>48</age>
</user>
<user>
<name>Larry Page</name>
<company>Google</company>
<age>48</age>
</user>
</users>

XML,
C#.

XML
System.Xml
XML C# .
XML
System.Xml.
, xml-:

XmlNode:

xml.

XmlDocument: xml-

XmlElement: .
XmlNode

XmlAttribute:

XmlText: ,
,

XmlComment: xml

XmlNodeList:

,
xml, XmlNode,
:

Attributes

XmlAttributeCollection,

ChildNodes

HasChildNodes true,

FirstChild

LastChild

InnerText

InnerXml

xml

Name

, <user> -

Name "user"

ParentNode

. xml

users.xml

1
2
3
4
5
6
7
8
9
10
11

<?xml version="1.0" encoding="utf-8" ?>


<users>
<user name="Bill Gates">
<company>Microsoft</company>
<age>48</age>
</user>
<user name="Larry Page">
<company>Google</company>
<age>42</age>
</user>
</users>

:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

using System.Xml;
class Program
{
static void Main(string[] args)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("users.xml");
//
XmlElement xRoot = xDoc.DocumentElement;
//
foreach(XmlNode xnode in xRoot)
{
// name
if(xnode.Attributes.Count>0)
{
XmlNode attr = xnode.Attributes.GetNamedItem("name");
if (attr!=null)
Console.WriteLine(attr.Value);
}
// user
foreach(XmlNode childnode in xnode.ChildNodes)
{
// - company
if(childnode.Name=="company")
{
Console.WriteLine(": {0}", childnode.InnerText);
}
// age
if (childnode.Name == "age")

30
31
32
33
34
35
36
37
38

{
Console.WriteLine(": {0}", childnode.InnerText);
}
}
Console.WriteLine();
}
Console.Read();
}
}

xml,
XmlDocument xml-:xDoc.Load("users.xml");
xml
xDoc.DocumentElement.
.
foreach(XmlNode xnode in xRoot)

<user>, : XmlNode attr =


xnode.Attributes.GetNamedItem("name");

: foreach(XmlNode childnode in xnode.ChildNodes)


, ,
: if(childnode.Name=="company")
User xml:
List<User> users = new List<User>();

XmlDocument xDoc = new XmlDocument();


xDoc.Load("users.xml");
XmlElement xRoot = xDoc.DocumentElement;
foreach(XmlElement xnode in xRoot)
{
User user = new User();
XmlNode attr = xnode.Attributes.GetNamedItem("name");
if (attr!=null)
user.Name=attr.Value;
foreach (XmlNode childnode in xnode.ChildNodes)
{
if (childnode.Name == "company")
user.Company=childnode.InnerText;
if (childnode.Name == "age")
user.Age = Int32.Parse(childnode.InnerText);
}
users.Add(user);
}
foreach (User u in users)
Console.WriteLine("{0} ({1}) - {2}", u.Name, u.Company, u.Age);

XML-
xml- (, ,
) XmlNode:

AppendChild:

InsertAfter:

InsertBefore:

RemoveAll:

RemoveChild:

XmlElement, XmlNode,
, :

CreateNode:

CreateElement: XmlDocument

CreateAttribute: XmlAttribute

CreateTextNode: XmlTextNode

CreateComment:

xml-
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

XmlDocument xDoc = new XmlDocument();


xDoc.Load("users.xml");
XmlElement xRoot = xDoc.DocumentElement;
// user
XmlElement userElem = xDoc.CreateElement("user");
// name
XmlAttribute nameAttr = xDoc.CreateAttribute("name");
// company age
XmlElement companyElem = xDoc.CreateElement("company");
XmlElement ageElem = xDoc.CreateElement("age");
//
XmlText nameText = xDoc.CreateTextNode("Mark Zuckerberg");
XmlText companyText = xDoc.CreateTextNode("Facebook");
XmlText ageText = xDoc.CreateTextNode("30");
//
nameAttr.AppendChild(nameText);
companyElem.AppendChild(companyText);
ageElem.AppendChild(ageText);
userElem.Attributes.Append(nameAttr);
userElem.AppendChild(companyElem);
userElem.AppendChild(ageElem);
xRoot.AppendChild(userElem);

24

xDoc.Save("users.xml");

.
(xDoc.CreateElement("user")). ,
, .

( XmlText

companyText

xDoc.CreateTextNode("Facebook");).

user,
(xRoot.AppendChild(userElem);).

Save: xDoc.Save("users.xml")
xml- :
1
2
3
4

<user name="Mark Zuckerberg">


<company>Facebook</company>
<age>30</age>
</user>


xml-:
1
2
3
4
5
6
7

XmlDocument xDoc = new XmlDocument();


xDoc.Load("users.xml");
XmlElement xRoot = xDoc.DocumentElement;
XmlNode firstNode = xRoot.FirstChild;
xRoot.RemoveChild(firstNode);
xDoc.Save("users.xml");

XPath
XPath XML. ,
.
:
.


..

*

user
, "user"
@name
, @ (
"name")
@+

element[3]
,
//user
"user"
user[@name='Bill Gates']
.
"user" name='Bill Gates'
user[company='Microsoft']
.
"user", "company"
'Microsoft'
//user/company
"company",
"user"
XPath XmlElement:

SelectSingleNode(): .
,

SelectNodes(): XmlNodeList

xml- :

1
2
3
4
5
6
7
8
9
10
11

<?xml version="1.0" encoding="utf-8" ?>


<users>
<user name="Bill Gates">
<company>Microsoft</company>
<age>48</age>
</user>
<user name="Larry Page">
<company>Google</company>
<age>42</age>
</user>
</users>

, user:

1
2
3
4
5
6
7
8

XmlDocument xDoc = new XmlDocument();


xDoc.Load("users.xml");
XmlElement xRoot = xDoc.DocumentElement;
//
XmlNodeList childnodes = xRoot.SelectNodes("*");
foreach (XmlNode n in childnodes)
Console.WriteLine(n.OuterXml);

<user>:

XmlNodeList childnodes = xRoot.SelectNodes("user");

name user:

1
2
3

XmlNodeList childnodes = xRoot.SelectNodes("user");


foreach (XmlNode n in childnodes)
Console.WriteLine(n.SelectSingleNode("@name").Value);

:
Bill Gates
Larry Page
, name "Bill Gates":

1
2
3

XmlNode childnode = xRoot.SelectSingleNode("user[@name='Bill Gates']");


if (childnode != null)
Console.WriteLine(childnode.OuterXml);

, "company" "Microsoft":

1
2
3

XmlNode childnode = xRoot.SelectSingleNode("user[company='Microsoft']");


if (childnode != null)
Console.WriteLine(childnode.OuterXml);

, .
:

1
2
3

XmlNodeList childnodes = xRoot.SelectNodes("//user/company");


foreach (XmlNode n in childnodes)
Console.WriteLine(n.InnerText);