简体   繁体   English

C#转义和签名

[英]C# Escaping & sign

I am writing ac# application the uses the Fishbowl Inventory API. 我正在编写使用Fishbowl库存API的ac#应用程序。

It works well but I am having problems when the customer name has special characters like "&". 它运作良好,但是当客户名称包含特殊字符(例如“&”)时,我遇到了问题。

For example: "spirits & wines, LLC" I need help escaping the "&" sign so my program doesn't crash. 例如:“ spirits&wines,LLC”我需要转义“&”符号的帮助,因此我的程序不会崩溃。

Here is my xml string. 这是我的xml字符串。

string customerFBCommand = "" +
            "<FbiXml>" +
                "<Ticket><Key>" + key + "</Key></Ticket>" +
                "<FbiMsgsRq> " +
                    "<CustomerSaveRq>" +
                        "<Customer>" +
                            "<Status>Normal</Status>" +
                            "<CustomerID>-1</CustomerID>" +
                            "<AccountID>-1</AccountID>" +
                            "<Name>" +  name + "</Name>" +
                            "<ActiveFlag>true</ActiveFlag>" +
                            "<JobDepth>1</JobDepth>" +
                            "<Addresses>" +
                                "<Address>" +
                                    "<Name>Billing Address</Name>" +
                                    //"<Attn>Attn</Attn>"+
                                    "<Street>" + billing[0] + " " + billing[1] + " " + billing[2] + "</Street>" +
                                    "<City>" + billing[4] + "</City>" +
                                    "<Zip>" + billing[3] + "</Zip>" +
                                    "<Default>true</Default>" +
                                    "<Residential>false</Residential>" +
                                    "<Type>Main Office</Type>" +
                                    "<State>" +
                                        "<Name> </Name>" +
                                        "<Code>"+ billing[5] +"</Code>" +
                                        "<CountryID>2</CountryID>" +
                                    "</State>" +
                                    "<Country>" +
                                        "<Name>"+ billing[6] +"</Name>" +
                                        "<Code>US</Code>" +
                                    "</Country>" +
                                    "<AddressInformationList>" +
                                        "<AddressInformation>"+
                                            "<Name>Billing Address</Name>"+
                                            "<Data>Address Data</Data>" +
                                            "<Default>true</Default>" +
                                            "<Type>Home</Type>" +
                                        "</AddressInformation>" +
                                    "</AddressInformationList>" +
                                "</Address>" +
                                "<Address>" +
                                    "<Name>Shipping Address</Name>" +
                                    "<Attn>Attn</Attn>" +
                                    "<Street>" + shipping[0] + " " + shipping[1] + " " + shipping[2] + "</Street>" +
                                    "<City>" + shipping[4] + "</City>" +
                                    "<Zip>" + shipping[3] + "</Zip>" +
                                    "<Default>false</Default>" +
                                    "<Residential>false</Residential>" +
                                    "<Type>Ship To</Type>" +
                                    "<State>" +
                                        "<Name> </Name>" +
                                        "<Code>" + shipping[5] + "</Code>" +
                                        "<CountryID>2</CountryID>" +
                                    "</State>" +
                                    "<Country>" +
                                        "<Name>" + shipping[6] + "</Name>" +
                                        "<Code>US</Code>" +
                                    "</Country>" +
                                    "<AddressInformationList>" +
                                        "<AddressInformation>" +
                                            "<Name>Shipping Address</Name>" +
                                            "<Data>Address Data</Data>" +
                                            "<Default>true</Default>" +
                                            "<Type>Home</Type>" +
                                        "</AddressInformation>" +
                                    "</AddressInformationList>" +
                                "</Address>" +
                            "</Addresses>" +
                        "</Customer>" +
                    "</CustomerSaveRq>" +
                "</FbiMsgsRq>" +
            "</FbiXml>" +
            "";

Thanks. 谢谢。

To escape strings for XML, you can use SecurityElement.Escape . 要转义XML的字符串,可以使用SecurityElement.Escape There's more than just & to escape. 不仅仅是&可以逃脱。

However, what I would suggest is to not build the XML by hand, but to use a proper DOM like XDocument to build it for you. 但是,我建议不要手工构建XML,而要使用像XDocument这样的适当DOM为您构建XML。

In XML and HTML, you escape it with &amp; 在XML和HTML中,您可以使用&amp;对其进行转义&amp; . So in your C# code you can write name.Replace("&", "&amp;") . 因此,在您的C#代码中,您可以编写name.Replace("&", "&amp;")

However a more robust solution would be to use an XML writer, which .NET has a library for. 但是,更健壮的解决方案是使用XML编写器,.NET为其提供一个库。 Or you could use HttpUtility.HtmlEncode(name); 或者您可以使用HttpUtility.HtmlEncode(name); . There are several solutions. 有几种解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM