将 XmlNode 添加到 XmlElement

发布于 2024-11-10 15:09:26 字数 1220 浏览 1 评论 0原文

我从网络服务中收到一个肥皂信封,其中包含姓名和地址等客户数据。该地址不包含城市/郊区,但包含邮政编码。我将所有城市和郊区及其邮政编码保存在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2024-11-17 15:09:26

好吧,如果没有实际看到现有的 XML 结构和所需的新 XML 结构,就很难知道。基本上,我假设您需要一个新的 XML 节点,其中包含与 postcode 元素处于同一级别的郊区。

在这种情况下,我使用了:

XmlElement elem = xDoc.CreateElement("suburb");
elem.InnerText = ...;
node.ParentNode.AppendChild(elem);

编辑
至于效率:为什么不只读取一次“邮政编码文件”,将条目添加到包含邮政编码作为键和郊区作为值的字典中?这比每次读取文件要快得多。

Dictionary<string, string> postCodeMap = new Dictionary<string, string>();
string[] lines = File.ReadAllLines(...);
foreach (string line in lines)
{
   string[] parts = line.Split(',');
   postCodeMap[parts[0]] = parts[1];
}

然后再做:

foreach (XmlNode node in postCodeNodes)
{ 
    string suburb = postCodeMap[node.InnerText];

    XmlElement elem = xDoc.CreateElement("suburb");
    elem.InnerText = suburb;
    node.ParentNode.AppendChild(elem);
}

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:

XmlElement elem = xDoc.CreateElement("suburb");
elem.InnerText = ...;
node.ParentNode.AppendChild(elem);

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.

Dictionary<string, string> postCodeMap = new Dictionary<string, string>();
string[] lines = File.ReadAllLines(...);
foreach (string line in lines)
{
   string[] parts = line.Split(',');
   postCodeMap[parts[0]] = parts[1];
}

And later do:

foreach (XmlNode node in postCodeNodes)
{ 
    string suburb = postCodeMap[node.InnerText];

    XmlElement elem = xDoc.CreateElement("suburb");
    elem.InnerText = suburb;
    node.ParentNode.AppendChild(elem);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文