.net读取XML文件问题 急!急!急!
<CATEGORYgeneration="2012-03-1609:00:04">-<LARGE_CATEGORYid="100000002"name="노...
<CATEGORY generation="2012-03-16 09:00:04">
- <LARGE_CATEGORY id="100000002" name="노트북/데스크탑">
- <MIDDLE_CATEGORY id="200000433" name="데스크탑 본체">
- <SMALL_CATEGORY id="300004032" name="삼성 Magic station">
- <CLASS name="CPU" NECESSARY="Y">
<CLASS_VALUE id="39471" name="INTEL" />
<CLASS_VALUE id="39472" name="AMD" />
</CLASS>
- <CLASS name="사양" NECESSARY="Y">
<CLASS_VALUE id="39481" name="쿼드코어" />
<CLASS_VALUE id="39482" name="트리플코어" />
<CLASS_VALUE id="39483" name="듀얼코어" />
<CLASS_VALUE id="39484" name="싱글코어" />
</CLASS>
- <CLASS name="모니터패키지" NECESSARY="Y">
<CLASS_VALUE id="39489" name="17" 모니터포함="" />
<CLASS_VALUE id="39490" name="19" 모니터포함="" />
<CLASS_VALUE id="39491" name="20" 모니터포함="" />
<CLASS_VALUE id="39492" name="22" 모니터포함="" />
<CLASS_VALUE id="39493" name="24" 모니터포함="" />
<CLASS_VALUE id="39494" name="기타" />
</CLASS>
- <CLASS name="그래픽카드3" NECESSARY="Y">
<CLASS_VALUE id="100004" name="그래픽" />
<CLASS_VALUE id="100005" name="그래픽2" />
<CLASS_VALUE id="100006" name="그래픽3" />
<CLASS_VALUE id="100007" name="그래픽4" />
<CLASS_VALUE id="100008" name="그래픽5" />
</CLASS>
- <CLASS name="메모리" NECESSARY="Y">
<CLASS_VALUE id="0" name="" />
</CLASS>
</SMALL_CATEGORY>
- <SMALL_CATEGORY id="300020974" name="테스트소분류">
- <CLASS name="11" NECESSARY="Y">
<CLASS_VALUE id="0" name="" />
</CLASS>
</SMALL_CATEGORY>
- <SMALL_CATEGORY id="300004037" name="기타브랜드">
- <CLASS name="CPU" NECESSARY="Y">
<CLASS_VALUE id="39574" name="INTEL" />
<CLASS_VALUE id="39575" name="AMD" />
</CLASS>
- <CLASS name="사양" NECESSARY="Y">
<CLASS_VALUE id="39580" name="쿼드코어" />
<CLASS_VALUE id="39581" name="트리플코어" />
<CLASS_VALUE id="39582" name="듀얼코어" />
<CLASS_VALUE id="39583" name="싱글코어" />
</CLASS>
</SMALL_CATEGORY>
......
我要获取没个节点里面的ID跟NAME值,求高手,求指教~!
求贴源码~! 展开
- <LARGE_CATEGORY id="100000002" name="노트북/데스크탑">
- <MIDDLE_CATEGORY id="200000433" name="데스크탑 본체">
- <SMALL_CATEGORY id="300004032" name="삼성 Magic station">
- <CLASS name="CPU" NECESSARY="Y">
<CLASS_VALUE id="39471" name="INTEL" />
<CLASS_VALUE id="39472" name="AMD" />
</CLASS>
- <CLASS name="사양" NECESSARY="Y">
<CLASS_VALUE id="39481" name="쿼드코어" />
<CLASS_VALUE id="39482" name="트리플코어" />
<CLASS_VALUE id="39483" name="듀얼코어" />
<CLASS_VALUE id="39484" name="싱글코어" />
</CLASS>
- <CLASS name="모니터패키지" NECESSARY="Y">
<CLASS_VALUE id="39489" name="17" 모니터포함="" />
<CLASS_VALUE id="39490" name="19" 모니터포함="" />
<CLASS_VALUE id="39491" name="20" 모니터포함="" />
<CLASS_VALUE id="39492" name="22" 모니터포함="" />
<CLASS_VALUE id="39493" name="24" 모니터포함="" />
<CLASS_VALUE id="39494" name="기타" />
</CLASS>
- <CLASS name="그래픽카드3" NECESSARY="Y">
<CLASS_VALUE id="100004" name="그래픽" />
<CLASS_VALUE id="100005" name="그래픽2" />
<CLASS_VALUE id="100006" name="그래픽3" />
<CLASS_VALUE id="100007" name="그래픽4" />
<CLASS_VALUE id="100008" name="그래픽5" />
</CLASS>
- <CLASS name="메모리" NECESSARY="Y">
<CLASS_VALUE id="0" name="" />
</CLASS>
</SMALL_CATEGORY>
- <SMALL_CATEGORY id="300020974" name="테스트소분류">
- <CLASS name="11" NECESSARY="Y">
<CLASS_VALUE id="0" name="" />
</CLASS>
</SMALL_CATEGORY>
- <SMALL_CATEGORY id="300004037" name="기타브랜드">
- <CLASS name="CPU" NECESSARY="Y">
<CLASS_VALUE id="39574" name="INTEL" />
<CLASS_VALUE id="39575" name="AMD" />
</CLASS>
- <CLASS name="사양" NECESSARY="Y">
<CLASS_VALUE id="39580" name="쿼드코어" />
<CLASS_VALUE id="39581" name="트리플코어" />
<CLASS_VALUE id="39582" name="듀얼코어" />
<CLASS_VALUE id="39583" name="싱글코어" />
</CLASS>
</SMALL_CATEGORY>
......
我要获取没个节点里面的ID跟NAME值,求高手,求指教~!
求贴源码~! 展开
3个回答
展开全部
XmlDocument doc = new XmlDocument();
doc.LoadXml("xml格式字符串");
然后操作doc 你的xml数据都在doc里面
doc.LoadXml("xml格式字符串");
然后操作doc 你的xml数据都在doc里面
追问
写一个简单的例子。。。刚接触,不咋会~!
追答
这一句是xml格式数据必须加下去的 如下
doc.LoadXml("");
//doc.ChildNodes 就是取得所有节点 然后下面自己想
foreach (XmlNode n in doc.ChildNodes)
{
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("加载你的XML文件");
foreach (XmlNode node in xmlDoc.SelectNodes("//CLASS_VALUE"))
{ //遍历每个CLASS_VALUE节点
string strID = node.Attributes["id"].Value;
string strName = node.Attributes["name"].Value;
}
xmlDoc.Load("加载你的XML文件");
foreach (XmlNode node in xmlDoc.SelectNodes("//CLASS_VALUE"))
{ //遍历每个CLASS_VALUE节点
string strID = node.Attributes["id"].Value;
string strName = node.Attributes["name"].Value;
}
更多追问追答
追问
这个需要转换编码格式对吧?如果是下一级的话怎么办呢?
追答
转换什么格式?XML里是什么字符串,拿到的就是什么字符串。
下一级指什么?你这里CLASS_VALUE已经是最下面一级的节点了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace RegexTest
{
public class RegexTest
{
public void Test()
{
string input = @"<CATEGORY generation="2012-03-16 09:00:04">
- <LARGE_CATEGORY id="100000002" name="노트북/데스크탑">
- <MIDDLE_CATEGORY id="200000433" name="데스크탑 본체">
- <SMALL_CATEGORY id="300004032" name="삼성 Magic station">
- <CLASS name="CPU" NECESSARY="Y">
<CLASS_VALUE id="39471" name="INTEL" />
<CLASS_VALUE id="39472" name="AMD" />
</CLASS>
- <CLASS name="사양" NECESSARY="Y">
<CLASS_VALUE id="39481" name="쿼드코어" />
<CLASS_VALUE id="39482" name="트리플코어" />
<CLASS_VALUE id="39483" name="듀얼코어" />
<CLASS_VALUE id="39484" name="싱글코어" />
</CLASS>
- <CLASS name="모니터패키지" NECESSARY="Y">
<CLASS_VALUE id="39489" name="17" 모니터포함="" />
<CLASS_VALUE id="39490" name="19" 모니터포함="" />
<CLASS_VALUE id="39491" name="20" 모니터포함="" />
<CLASS_VALUE id="39492" name="22" 모니터포함="" />
<CLASS_VALUE id="39493" name="24" 모니터포함="" />
<CLASS_VALUE id="39494" name="기타" />
</CLASS>
- <CLASS name="그래픽카드3" NECESSARY="Y">
<CLASS_VALUE id="100004" name="그래픽" />
<CLASS_VALUE id="100005" name="그래픽2" />
<CLASS_VALUE id="100006" name="그래픽3" />
<CLASS_VALUE id="100007" name="그래픽4" />
<CLASS_VALUE id="100008" name="그래픽5" />
</CLASS>
- <CLASS name="메모리" NECESSARY="Y">
<CLASS_VALUE id="0" name="" />
</CLASS>
</SMALL_CATEGORY>
- <SMALL_CATEGORY id="300020974" name="테스트소분류">
- <CLASS name="11" NECESSARY="Y">
<CLASS_VALUE id="0" name="" />
</CLASS>
</SMALL_CATEGORY>
- <SMALL_CATEGORY id="300004037" name="기타브랜드">
- <CLASS name="CPU" NECESSARY="Y">
<CLASS_VALUE id="39574" name="INTEL" />
<CLASS_VALUE id="39575" name="AMD" />
</CLASS>
- <CLASS name="사양" NECESSARY="Y">
<CLASS_VALUE id="39580" name="쿼드코어" />
<CLASS_VALUE id="39581" name="트리플코어" />
<CLASS_VALUE id="39582" name="듀얼코어" />
<CLASS_VALUE id="39583" name="싱글코어" />
</CLASS>
</SMALL_CATEGORY>
";
string pattern = @"id="(?<id>[^"]+)"\s*name="(?<name>[^"]+)";
RegexOptions options = RegexOptions.None | RegexOptions.IgnoreCase | RegexOptions.Singleline;
Regex regex = new Regex(pattern, options);
MatchCollection matches = regex.Matches(input);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups["id"].value + "\t" + match.Groups["name"].value );
}
}
}
}
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace RegexTest
{
public class RegexTest
{
public void Test()
{
string input = @"<CATEGORY generation="2012-03-16 09:00:04">
- <LARGE_CATEGORY id="100000002" name="노트북/데스크탑">
- <MIDDLE_CATEGORY id="200000433" name="데스크탑 본체">
- <SMALL_CATEGORY id="300004032" name="삼성 Magic station">
- <CLASS name="CPU" NECESSARY="Y">
<CLASS_VALUE id="39471" name="INTEL" />
<CLASS_VALUE id="39472" name="AMD" />
</CLASS>
- <CLASS name="사양" NECESSARY="Y">
<CLASS_VALUE id="39481" name="쿼드코어" />
<CLASS_VALUE id="39482" name="트리플코어" />
<CLASS_VALUE id="39483" name="듀얼코어" />
<CLASS_VALUE id="39484" name="싱글코어" />
</CLASS>
- <CLASS name="모니터패키지" NECESSARY="Y">
<CLASS_VALUE id="39489" name="17" 모니터포함="" />
<CLASS_VALUE id="39490" name="19" 모니터포함="" />
<CLASS_VALUE id="39491" name="20" 모니터포함="" />
<CLASS_VALUE id="39492" name="22" 모니터포함="" />
<CLASS_VALUE id="39493" name="24" 모니터포함="" />
<CLASS_VALUE id="39494" name="기타" />
</CLASS>
- <CLASS name="그래픽카드3" NECESSARY="Y">
<CLASS_VALUE id="100004" name="그래픽" />
<CLASS_VALUE id="100005" name="그래픽2" />
<CLASS_VALUE id="100006" name="그래픽3" />
<CLASS_VALUE id="100007" name="그래픽4" />
<CLASS_VALUE id="100008" name="그래픽5" />
</CLASS>
- <CLASS name="메모리" NECESSARY="Y">
<CLASS_VALUE id="0" name="" />
</CLASS>
</SMALL_CATEGORY>
- <SMALL_CATEGORY id="300020974" name="테스트소분류">
- <CLASS name="11" NECESSARY="Y">
<CLASS_VALUE id="0" name="" />
</CLASS>
</SMALL_CATEGORY>
- <SMALL_CATEGORY id="300004037" name="기타브랜드">
- <CLASS name="CPU" NECESSARY="Y">
<CLASS_VALUE id="39574" name="INTEL" />
<CLASS_VALUE id="39575" name="AMD" />
</CLASS>
- <CLASS name="사양" NECESSARY="Y">
<CLASS_VALUE id="39580" name="쿼드코어" />
<CLASS_VALUE id="39581" name="트리플코어" />
<CLASS_VALUE id="39582" name="듀얼코어" />
<CLASS_VALUE id="39583" name="싱글코어" />
</CLASS>
</SMALL_CATEGORY>
";
string pattern = @"id="(?<id>[^"]+)"\s*name="(?<name>[^"]+)";
RegexOptions options = RegexOptions.None | RegexOptions.IgnoreCase | RegexOptions.Singleline;
Regex regex = new Regex(pattern, options);
MatchCollection matches = regex.Matches(input);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups["id"].value + "\t" + match.Groups["name"].value );
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询