将 XmlNode 添加到 XmlElement
我从网络服务中收到一个肥皂信封,其中包含姓名和地址等客户数据。该地址不包含城市/郊区,但包含邮政编码。我将所有城市和郊区及其邮政编码保存在 CSV 文件中,因此我想为每个邮政编码插入正确的名称。我可以将其存储在数据库或其他东西中,但这更多的是关于如何在传递数据之前插入节点。
代码就像:
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(searchResponse);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("ns", wsNamespace);
XmlNodeList postCodeNodes = xDoc.SelectNodes("//ns:postcode", nsmgr);
string applicationPath = AppDomain.CurrentDomain.BaseDirectory;
foreach (XmlNode node in postCodeNodes)
{
using (StreamReader readFile = new StreamReader(applicationPath + "postcodes.csv"))
{
string line;
string[] row;
while ((line = readFile.ReadLine()) != null)
{
row = line.Split(',');
if (row[0].ToString() == node.InnerText)
{
string suburb = row[1].ToString();
//XmlNode ndSuburb = xDoc.CreateElement("suburb");
//ndSuburb.Value = suburb;
//node.ParentNode.AppendChild(ndSuburb);
break;
}
}
}
}
并且我不知道在注释掉代码的地方该怎么做。有什么建议吗?有关如何提高效率的建议也将受到赞赏。
提前致谢。
I get a soap envelope back from a web service with customer data such as name and address etc. The address does not contain city/suburb but postcode. I have all the city and suburbs with their post codes in a CSV file so I want to insert the correct name for each post code. I can store it in a database or something else but this is more about how to insert the node before I pass the data on.
The code is like :
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(searchResponse);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xDoc.NameTable);
nsmgr.AddNamespace("ns", wsNamespace);
XmlNodeList postCodeNodes = xDoc.SelectNodes("//ns:postcode", nsmgr);
string applicationPath = AppDomain.CurrentDomain.BaseDirectory;
foreach (XmlNode node in postCodeNodes)
{
using (StreamReader readFile = new StreamReader(applicationPath + "postcodes.csv"))
{
string line;
string[] row;
while ((line = readFile.ReadLine()) != null)
{
row = line.Split(',');
if (row[0].ToString() == node.InnerText)
{
string suburb = row[1].ToString();
//XmlNode ndSuburb = xDoc.CreateElement("suburb");
//ndSuburb.Value = suburb;
//node.ParentNode.AppendChild(ndSuburb);
break;
}
}
}
}
and I am not sure what to do where I have commented out the code. Any suggestions? Tips on how to make this more efficient would also be appreciated.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果没有实际看到现有的 XML 结构和所需的新 XML 结构,就很难知道。基本上,我假设您需要一个新的 XML 节点,其中包含与
postcode
元素处于同一级别的郊区。在这种情况下,我使用了:
编辑
至于效率:为什么不只读取一次“邮政编码文件”,将条目添加到包含邮政编码作为键和郊区作为值的字典中?这比每次读取文件要快得多。
然后再做:
Well, it's a bit hard to know without actually seeing the XML structure that exists and the desired new XML structure. Basically I'd assume that you want a new XML node containing the suburb at the same level as the
postcode
element.In that case, I'd used:
EDIT
As for efficiency: Why don't you read your "postcode file" only once, adding the entries to a dictionary that contains the post code as a key and the suburb as a value? That's far quicker than reading the file every time.
And later do: