62,271
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8" ?>
- <Contacts>
- <Contact>
<ID>848495f7-513d-4a40-b417-01b93e8c2cee</ID>
<WindowsLiveID>aaa@msn.com</WindowsLiveID>
<AutoUpdateEnabled>false</AutoUpdateEnabled>
<LastChanged>2006-03-16T03:12:57.8800000Z</LastChanged>
- <Profiles>
- <Personal>
<UniqueName>asd</UniqueName>
<SortName>asdsad</SortName>
<NickName>Chense</NickName>
<DisplayName>Mname</DisplayName>
</Personal>
</Profiles>
- <Emails>
- <Email>
<ID>8</ID>
<EmailType>WindowsLiveID</EmailType>
<Address>askdaa@sina.com</Address>
<IsIMEnabled>true</IsIMEnabled>
<IsDefault>false</IsDefault>
</Email>
</Emails>
</Contact>
- <Contact>
<ID>848495f7-513d-4a40-b417-01b93e8c2cee</ID>
<WindowsLiveID>aaa@msn.com</WindowsLiveID>
<AutoUpdateEnabled>false</AutoUpdateEnabled>
<LastChanged>2006-03-16T03:12:57.8800000Z</LastChanged>
- <Profiles>
- <Personal>
<UniqueName>asd</UniqueName>
<SortName>asdsad</SortName>
<NickName>Chense</NickName>
<DisplayName>Mname</DisplayName>
</Personal>
</Profiles>
- <Emails>
- <Email>
<ID>8</ID>
<EmailType>WindowsLiveID</EmailType>
<Address>askdaa@sina.com</Address>
<IsIMEnabled>true</IsIMEnabled>
<IsDefault>false</IsDefault>
</Email>
</Emails>
</Contact>
</Contacts>
static void Main(string[] args)
{
XPathNavigator xPathNavigator;
XPathDocument xPathDoc;
xPathDoc = new XPathDocument("C:\\test.xml");
xPathNavigator = xPathDoc.CreateNavigator();
xPathNavigator.MoveToRoot();
xPathNavigator.MoveToFirstChild();
ReturnValue(xPathNavigator);
Console.ReadKey();
/*
ID: 848495f7-513d-4a40-b417-01b93e8c2cee
WindowsLiveID: aaa@msn.com
AutoUpdateEnabled: false
LastChanged: 2006-03-16T03:12:57.8800000Z
Profiles: asdasdsadChenseMname
UniqueName: asd
SortName: asdsad
NickName: Chense
DisplayName: Mname
Emails: 8WindowsLiveIDaskdaa@sina.comtruefalse
ID: 8
EmailType: WindowsLiveID
Address: askdaa@sina.com
IsIMEnabled: true
IsDefault: false
*
ID: 848495f7-513d-4a40-b417-01b93e8c2cee
WindowsLiveID: aaa@msn.com
AutoUpdateEnabled: false
LastChanged: 2006-03-16T03:12:57.8800000Z
Profiles: asdasdsadChenseMname
UniqueName: asd
SortName: asdsad
NickName: Chense
DisplayName: Mname
Emails: 8WindowsLiveIDaskdaa@sina.comtruefalse
ID: 8
EmailType: WindowsLiveID
Address: askdaa@sina.com
IsIMEnabled: true
IsDefault: false
*/
}
static XPathNavigator ReturnValue(XPathNavigator xPathNavigator)
{
if (xPathNavigator.NodeType == XPathNodeType.Element)
{
if (xPathNavigator.HasChildren)
{
xPathNavigator.MoveToFirstChild();
do
{
if (xPathNavigator.HasChildren)
{
xPathNavigator.MoveToFirstChild();
do
{
Console.Write("{0}:\t{1}\r\n", xPathNavigator.Name, xPathNavigator.Value);
ReturnValue(xPathNavigator);
}
while (xPathNavigator.MoveToNext());
xPathNavigator.MoveToParent();
}
} while (xPathNavigator.MoveToNext());
xPathNavigator.MoveToParent();
}
}
return xPathNavigator;
} static void Main(string[] args)
{
XPathNavigator xPathNavigator;
XPathDocument xPathDoc;
xPathDoc = new XPathDocument("C:\\test.xml");
xPathNavigator = xPathDoc.CreateNavigator();
xPathNavigator.MoveToRoot();
xPathNavigator.MoveToFirstChild();
if (xPathNavigator.NodeType == XPathNodeType.Element)
{
if (xPathNavigator.HasChildren == true)
{
xPathNavigator.MoveToFirstChild();
do
{
if (xPathNavigator.HasChildren)
{
xPathNavigator.MoveToFirstChild();
do
{
Console.Write("{0}:\t{1}\r\n", xPathNavigator.Name, xPathNavigator.Value);
} while (xPathNavigator.MoveToNext());
xPathNavigator.MoveToParent();
}
Console.WriteLine("\r\n");
} while (xPathNavigator.MoveToNext());
}
}
Console.ReadKey();
}
/*
ID: 848495f7-513d-4a40-b417-01b93e8c2cee
WindowsLiveID: aaa@msn.com
AutoUpdateEnabled: false
LastChanged: 2006-03-16T03:12:57.8800000Z
Profiles: asdasdsadChenseMname
Emails: 8WindowsLiveIDaskdaa@sina.comtruefalse
ID: 848495f7-513d-4a40-b417-01b93e8c2cee
WindowsLiveID: aaa@msn.com
AutoUpdateEnabled: false
LastChanged: 2006-03-16T03:12:57.8800000Z
Profiles: asdasdsadChenseMname
Emails: 8WindowsLiveIDaskdaa@sina.comtruefalse
*/
string resHtml=@" 加入你的XML "; XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resHtml);
XmlNodeList xnl = xmlDoc.SelectSingleNode("Contacts").ChildNodes;
string aa = string.Empty;
foreach (XmlNode linke in xnl)
{
aa += linke.InnerText;
}
void Main()
{
string xml = @" <Contacts>
<Contact>
<ID>848495f7-513d-4a40-b417-01b93e8c2cee</ID>
<WindowsLiveID>aaa@msn.com</WindowsLiveID>
<AutoUpdateEnabled>false</AutoUpdateEnabled>
<LastChanged>2006-03-16T03:12:57.8800000Z</LastChanged>
<Profiles>
<Personal>
<UniqueName>asd</UniqueName>
<SortName>asdsad</SortName>
<NickName>Chense</NickName>
<DisplayName>Mname</DisplayName>
</Personal>
</Profiles>
<Emails>
<Email>
<ID>8</ID>
<EmailType>WindowsLiveID</EmailType>
<Address>askdaa@sina.com</Address>
<IsIMEnabled>true</IsIMEnabled>
<IsDefault>false</IsDefault>
</Email>
</Emails>
</Contact>
<Contact>
<ID>848495f7-513d-4a40-b417-01b93e8c2cee</ID>
<WindowsLiveID>aaa@msn.com</WindowsLiveID>
<AutoUpdateEnabled>false</AutoUpdateEnabled>
<LastChanged>2006-03-16T03:12:57.8800000Z</LastChanged>
<Profiles>
<Personal>
<UniqueName>asd</UniqueName>
<SortName>asdsad</SortName>
<NickName>Chense</NickName>
<DisplayName>Mname</DisplayName>
</Personal>
</Profiles>
<Emails>
<Email>
<ID>8</ID>
<EmailType>WindowsLiveID</EmailType>
<Address>askdaa@sina.com</Address>
<IsIMEnabled>true</IsIMEnabled>
<IsDefault>false</IsDefault>
</Email>
</Emails>
</Contact>
</Contacts>";
XElement xmlPage = XElement.Parse(xml);
var q = from x in xmlPage.Descendants("Contact")
select new
{
ID=x.Element("ID").Value,
WindowsLiveID = x.Element("WindowsLiveID").Value,
AutoUpdateEnabled = x.Element("AutoUpdateEnabled").Value,
LastChanged = x.Element("LastChanged").Value,
UniqueName = x.Element("Profiles").Element("Personal").Element("UniqueName").Value,
SortName = x.Element("Profiles").Element("Personal").Element("SortName").Value,
NickName = x.Element("Profiles").Element("Personal").Element("NickName").Value,
DisplayName = x.Element("Profiles").Element("Personal").Element("DisplayName").Value,
EmailID = x.Element("Emails").Element("Email").Element("ID").Value,
EmailType = x.Element("Emails").Element("Email").Element("EmailType").Value,
Address = x.Element("Emails").Element("Email").Element("Address").Value,
IsIMEnabled = x.Element("Emails").Element("Email").Element("IsIMEnabled").Value,
IsDefault = x.Element("Emails").Element("Email").Element("IsDefault").Value
};
Console.WriteLine("------------------");
foreach(var q1 in q)
{
Console.WriteLine(q1.ID);
Console.WriteLine(q1.WindowsLiveID);
Console.WriteLine(q1.AutoUpdateEnabled);
Console.WriteLine(q1.LastChanged);
Console.WriteLine(q1.UniqueName);
Console.WriteLine(q1.SortName);
Console.WriteLine(q1.NickName);
Console.WriteLine(q1.DisplayName);
Console.WriteLine(q1.EmailID);
Console.WriteLine(q1.EmailType);
Console.WriteLine(q1.Address);
Console.WriteLine(q1.IsIMEnabled);
Console.WriteLine(q1.IsDefault);
Console.WriteLine("------------------");
}
}
/*
------------------
848495f7-513d-4a40-b417-01b93e8c2cee
aaa@msn.com
false
2006-03-16T03:12:57.8800000Z
asd
asdsad
Chense
Mname
8
WindowsLiveID
askdaa@sina.com
true
false
------------------
848495f7-513d-4a40-b417-01b93e8c2cee
aaa@msn.com
false
2006-03-16T03:12:57.8800000Z
asd
asdsad
Chense
Mname
8
WindowsLiveID
askdaa@sina.com
true
false
------------------
*/