新手试图读取XML [英] Newbie trying to Read XML

查看:54
本文介绍了新手试图读取XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我已经在.xml文件中写了一堆共享设置而我正在尝试将它们读回来,不过我是我只获得第一个电子邮件地址和启用设置。 XML如下所示:

 <   SharedSettings  >  
< 电子邮件 >
< eMailAddress > gm111@verizon.net < / eMailAddress >
< eMailAddress > dq222@verizon.net < / eMailAddress >
< eMailAddress > fred333@verizon.net< / eMailAddress >
< eMailAddress / >
< eMailAddress / >
< eMailAddress / >
< eMailAddress / >
< eMailAddress / >
< eMailAddress / >
< eMailAddress / >
< eMailEnabled > True < / eMailEnabled >
< eMailEnabled > 错误< / eMailEnabled >
< eMailEnabled > 错误< / eMailEnabled >
< eMailEnabl ed > False < / eMailEnabled >
< eMailEnabled > False < / eMailEnabled >
< eMailEnabled > False < / eMailEnabled >
< eMailEnabled > False < / eMailEnabled >
< eMailEnabled > False < / eMailEnabled >
< eMailEnabled > False < / eMailEnabled >
< eMailEnabled > False < / eMailEnabled >
< / eMail >
< / SharedSettings >





和代码I用于阅读设置如下:



 xmlDoc =  new  XmlDocument(); 
xmlDoc.Load(@ GetSet.path + @ \ SharedSettings.xml) ;

XmlNodeList xnList = xmlDoc.SelectNodes( / SharedSettings / eMail) ;
x = 1 ;
foreach (XmlNode xn in xnList)
{
string eMailAddress [x] = xn [ eMailAddress]的innerText;
bool bEmail [x] = Convert.ToBoolean(xn [ eMailEnabled]的innerText)。
x ++;
}





查看xn中的Count它显示为一个。我是XML的新手,并没有看到我做错了什么。 XML错了还是我读错了?你有一个如何做到这一点的例子吗?



谢谢你,

格伦

解决方案

这里有几点......



首先是你的xml。您的电子邮件节点不能依赖于为每个电子邮件地址返回正确的启用设置...您基本上有随机数据,但重要的是,您只有< b>一个电子邮件节点 - 这就是为什么你的计数保持为1而你的代码只读取 eMailAddress eMailEnabled <的第一个实例/ code>



将xml构建成子节点...每个email都有一个地址和一个启用的设置。这样的东西可以工作

 <?  xml     version   =  1.0    encoding   =  utf-8  >  
< SharedSettings >
< 电子邮件 >
< eMailAddress > gm111@verizon.net < / eMailAddress >
< eMailEnabled > True < / eMailEnabled >
< / eMail >
< 电子邮件 >
< eMailAddress > dq222@verizon.net < / eMailAddress >
< < span class =code-leadattribute> eMailEnabled > False < / eMailEnabled >
< < span class =code-leadattribute> / eMail >
< 电子邮件 >
< eMailAddress > fred333@verizon.net < / eMailAddress >
< eMailEnabled > False < / eMail已启用 >
< / eMail >
< / SharedSettings >





由方式 - 不需要所有那些空节点,它们只是占用空间。



现在你的代码...我甚至无法得到这个编译 - 即使在我提供了 xmlDoc x

的声明之后我更正了eMailAddress和bMail的声明就像这样(在循环之外)

  string  [] eMailAddress =  new   string  [xnList.Count]; 
bool [] bEmail = new bool [xnList.Count];

注意 - 执行此操作时,您必须检查 xnList.Count 是否为零

结合上面对XML的更改,一切都开始起作用了。

为了完整性,这里是我最终得到的代码

 XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load(@\SharedSettings.xml);
XmlNodeList xnList = xmlDoc.SelectNodes(/ SharedSettings / eMail);

if(xnList.Count> 0)
{
string [] eMailAddress = new string [xnList.Count];
bool [] bEmail = new bool [xnList.Count];
int x = 1;
foreach(xnList中的XmlNode xn)
{
eMailAddress [x] = xn [eMailAddress]。InnerText;
bEmail [x] = Convert.ToBoolean(xn [eMailEnabled]。InnerText);
x ++;
}
}





顺便说一句,如果你是的话,你会发现XMLReader更有效率只是直接阅读这样的xml ...看看这个使用XmlReader用C#上课 [ ^ ]


Hi,

I have written a bunch of "Shared Settings" out to an .xml file and I'm trying to read them back, however I'm only getting the first email address and enabled setting. The XML looks like this:

<SharedSettings>
<eMail>
 <eMailAddress>gm111@verizon.net</eMailAddress>
 <eMailAddress>dq222@verizon.net</eMailAddress>
 <eMailAddress>fred333@verizon.net</eMailAddress>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailAddress/>
 <eMailEnabled>True</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 <eMailEnabled>False</eMailEnabled>
 </eMail>
</SharedSettings>



and the code I'm using to read the settings looks like this:

xmlDoc = new XmlDocument();
xmlDoc.Load(@GetSet.path + @"\SharedSettings.xml");

XmlNodeList xnList = xmlDoc.SelectNodes("/SharedSettings/eMail");
x = 1;
foreach (XmlNode xn in xnList)
{
    string eMailAddress[x] = xn["eMailAddress"].InnerText;
    bool bEmail[x] = Convert.ToBoolean(xn["eMailEnabled"].InnerText);
    x++;
}



Looking at the Count in "xn" it shows as one. I'm new to XML and don't see what I've done wrong. Is the XML wrong or am I reading it wrong? Do you have an example of how to do this?

Thank you,
Glenn

解决方案

Couple of points here ...

Firstly your xml. Your eMail node can't be relied upon to return the correct enabled setting for each email address ... you essentially have random data in there, but importantly, you only have one eMail node - hence why your count stays at 1 and your code only reads the first instance each of eMailAddress and eMailEnabled

Structure your xml into sub-nodes ... each "email" has an address AND a enabled setting. Something like this would work

<?xml version="1.0" encoding="utf-8"?>
<SharedSettings>
    <eMail>
        <eMailAddress>gm111@verizon.net</eMailAddress>
        <eMailEnabled>True</eMailEnabled>
    </eMail>
    <eMail>
        <eMailAddress>dq222@verizon.net</eMailAddress>
        <eMailEnabled>False</eMailEnabled>
    </eMail>
    <eMail>
        <eMailAddress>fred333@verizon.net</eMailAddress>
        <eMailEnabled>False</eMailEnabled>
    </eMail>
</SharedSettings>



By the way - there is no need for all those empty nodes, they're just taking up space.

Now to your code ... I couldn't even get this to compile - even after I provided a declaration for xmlDoc and x
So I corrected the declarations of eMailAddress and bMail like this (outside the loop)

string[]  eMailAddress = new string[xnList.Count];
bool[] bEmail = new bool[xnList.Count];

Note - when you do this you will have to check that xnList.Count is not zero
Combined with the changes to the XML above, that all started to work.
For completeness here is the code I ended up with

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"\SharedSettings.xml");
XmlNodeList xnList = xmlDoc.SelectNodes("/SharedSettings/eMail");

if ( xnList.Count > 0 )
{
    string[]  eMailAddress = new string[xnList.Count];
    bool[] bEmail = new bool[xnList.Count];
    int x = 1;
    foreach (XmlNode xn in xnList)
    {
        eMailAddress[x] = xn["eMailAddress"].InnerText;
        bEmail[x] = Convert.ToBoolean(xn["eMailEnabled"].InnerText);
        x++;
    }
}



As an aside, you might find XMLReader more efficient if you are just reading straight through xml like this ... have a look at this Using the XmlReader class with C#[^]


这篇关于新手试图读取XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆